What have I done wrong with gravity and movement?

Hi, so I’ve been having a couple of problems with this script. I got player movement working and have been trying to add a jump.

  • The first problem is the jump isn’t smooth. The object shoots up in what seems like 1 frame and then falls down.

  • Second is that the jump doesn’t work when standing still.

A couple of things I’ve managed to figure out is that when standing still the controller doesn’t read as grounded. Also that when i take out the Transform.translate line at the bottom it makes the controller be grounded when still, but then it can only move in x and z when off the ground.

    public float speeed = 8f;
    private float verticalVelocity;
    private float jumpForce = 40f;
    private float gravity = 50f;
    private Vector3 movement;
    private Vector3 moveRotation;
    void Update()

        CharacterController controller = gameObject.GetComponent<CharacterController>();

        float moveH = Input.GetAxis("Horizontal");
        float moveV = Input.GetAxis("Vertical");
        movement = new Vector3(moveH, verticalVelocity, moveV);
        moveRotation = new Vector3(moveH, 0, moveV);

        verticalVelocity = -gravity * Time.deltaTime;

        if (controller.isGrounded)
            print("CharacterController is grounded");
            verticalVelocity = -gravity * Time.deltaTime;
            if (Input.GetButtonDown("Jump"))

                verticalVelocity = jumpForce;


        //character rotation
        if (moveRotation != Vector3.zero)
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveRotation), 0.15f);

        // this line is what makes the controller not be grounded when standing still
        transform.Translate(movement * speeed * Time.deltaTime, Space.World); 

        controller.Move(movement * speeed * Time.deltaTime);

Have you tried just using unity’s physics engine. By default unity applies realistic gravity.

i think if you just add rigidbody to the object . then jump on button down and change y-axis like
transform.GetComponent().velocity = Vector3.up * speed;

Rather than using isGrounded, I’ve used a physics.raycast to detect the floor which allows you to jump when standing still. But I’m still having problems with a smooth jump.

When the jump button is pressed the player teleport’s upwards and the gravity gradually pulls them back down. Its probably something to do with how I’ve coded gravity.

Thanks for the help so far. Any more would be appreciated :slight_smile: