0
\$\begingroup\$

So I just copy and pasted the unity example class to use with my character controller.

public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Update() {
    CharacterController controller = GetComponent<CharacterController>();
    if (controller.isGrounded) {
        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;
        if (Input.GetButton("Jump"))
            moveDirection.y = jumpSpeed;

    }
    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime);
}

The code seems to work okay if I comment out the isGrounded check statement (which means that gravity isn't being applied). So what could be the reason that the gravity is not being applied to my character controller?

\$\endgroup\$
3
  • \$\begingroup\$ Code has very little effect on Gravity. Can you post screenshot of your character in inespector? Please post screenshot showing settings of character only. \$\endgroup\$
    – SanSolo
    Commented Jul 2, 2015 at 5:14
  • \$\begingroup\$ check the code that sets "controller.isGrounded" \$\endgroup\$ Commented Jul 2, 2015 at 8:17
  • \$\begingroup\$ From the way your script looks, gravity is applied regardless of the state of controller.isGrounded \$\endgroup\$
    – Joe
    Commented Jul 3, 2015 at 8:26

1 Answer 1

1
\$\begingroup\$

It looks like time scaling could be an issue here: you scale gravity by Time.deltaTime and apply that to the y axis of the movement vector. You then take that vector and scale it by Time.deltaTime.

This means that the y axis has been scaled twice. Given that there is usually very little time between Updates, you'll probably find that this means moveDirection.y is an infinismall number which gets rounded out to 0.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .