I’m making a player with CharacterController and having trouble with handling gravity.
Here’s the code:
void Update() {
Vector3 movement = m_MovementJoystick.Movement;
m_ForwardMovement = movement.z;
m_SideMovement = movement.x;
}
void FixedUpdate() {
Vector3 movement = new Vector3(m_SideMovement, 0, m_ForwardMovement);
movement *= m_WalkSpeed;
movement += Physics.gravity * Time.fixedDeltaTime;
CharacterController.Move(movement);
}
I’m calculating movement in the FixedUpdate. After adding this script and try dragging the player up from the scene view to check the adding gravity works, but the player just sticks in the ground! Even I manually change the Y position from inspector something like 50000, doesn’t make any movement!
First I thought that just adding gravity was too strong, so I tried to multiply various variables, but even I multiply very tiny numbers like 0.0001f, still, the player just sticks to the ground.
However, deleting the ground makes the player fall. This makes me really confusing, if the gravity works, why I can’t move up the player object?
Also in the unity’s official doc, they posted an example code to use “Move” in the “Update” method, not “FixedUpdate”.
As far as I know, all physics related to should be in the FixedUpdate, but why is the example code using Update?
CharacterController component operates outside of the Physics engine, so do not use the FixedUpdate. You said that deleting the ground makes you character fall - does it have a Rigidbody component? If yes then remove it. Do not mix physics system with CharacterController unless you exactly know what you are doing.
I recommend you to watch some tutorials on CharacterController
No, it doesn’t have a rigidbody. Only CharacterController and my script attached. I thought that CharacterController is working with Physics because it inherits collider class. Moving the gravity calculation into the update method seems to work. Thanks.