#Issues with character controller jumping

1 messages · Page 1 of 1 (latest)

austere gull
#

Hello! I've got a few issues, I'll put them below:

  • If I start holding space to jump, my game lags quite a bit (I think it's worse on my compyter than the video shows). This only happens when actually holding the button down.
  • If I jump at a wall and move after, I can get stuck in the side of the wall.
  • If i jump at the correct time into a wall, I sort of float up above the floor for a bit? I have a second jump almost, but it's a strange floaty one
  • Most importantly, my character jumps way higher in reality than it looks, allowing it to both feel unsatisfying as you barely look like you're jumping, and for you to slide up edges when it doesn't look like you can reach.

I'm not exactly sure how fixing this works since it isn't anything actually wrong with the code itself (as in errors), so any help would be appreciated. Apologies for the messy code, I was going to fix it up after- sending it in a second since this post is too long with it (sorry if this is a bas post, i'm very new to unity and code ettiquette!)

#
using System.Threading;
using Unity.VisualScripting;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.InputSystem;

public class playerMove : MonoBehaviour
{

    // creating values
    [SerializeField] float walkSpeed = 5f;
    [SerializeField] float sprintMult = 2;
    float Speed, Run;
    [SerializeField] float jumpForce = 4f;
    public CharacterController controller;
    float moveX, moveZ;
    public float Gravity = -15f;
    public float jumpCooldown;
    public Transform orientation;
    private Vector3 Velocity;
    Vector3 moveDir;
    public bool Grounded, readyToJump;
    public LayerMask ground;
    public float distance;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        controller = GetComponent<CharacterController>();
        ground = 1000000;
        readyToJump = true;
    }

    // Update is called once per frame
    void Update()
    {
        RaycastHit hit;
        Grounded = Physics.SphereCast(transform.position, 0.5f, Vector3.down, out hit, distance, ground);

        moveX = Input.GetAxisRaw("Horizontal");
        moveZ = Input.GetAxisRaw("Vertical");

        moveDir = orientation.forward * moveZ + orientation.right * moveX;
        moveDir.Normalize();

        Speed = walkSpeed;
        if (Input.GetAxisRaw("Sprint") > 0)
        {
            Speed *= sprintMult;
        }

        controller.Move(moveDir * Speed * Time.deltaTime);

        if (Input.GetKey(KeyCode.Space) && Grounded && readyToJump)
        {
            readyToJump = false;

            Jump();

            Invoke(nameof(ResetJump), jumpCooldown);
        }
        else if (Grounded == false)
        {
            Velocity.y += Gravity * Time.deltaTime;
        }
        controller.Move(Velocity * Time.deltaTime);
    }
#
    private void Jump()
    {
        // reset y velocity
        Velocity.y = 0f;

        Velocity.y = jumpForce;
    }
    private void ResetJump()
    {
        readyToJump = true;
    }

}
lucid flicker
#

Pretty sure you want GetKeyDown for the jump part not GetKey

austere gull
#

Also sorry, but changing it to GetKeyDown didn't fix any of the problems, and it breaks the code that allws me to keep jumping if I hold space down