I'm making a script in unity to control (movement) a character in 3rd person and 3D space.
For that I'm using the CharacterController
component.
All pretty standard: inside the update method a Vector3
which will be passed to CharacterController.move
is crafted based on the input and collisions, movement and jumping are perfectly fine but I ran into an issue when trying to make a wall jump.
There are 3 steps involved in this:
- Get the wall to jump of.
- Check if the player pres the jump button.
- Move the player in the desired direction.
An this is how I'm doing it:
void OnControllerColliderHit(ControllerColliderHit hit) {
if(mController.collisionFlags==CollisionFlags.Sides) {
//Get wall normal
wallNormal=hit.normal;
isOnAWall=true;
}
}
Now inside Update
if(Input.GetButtonDown("Jump")&isOnAWall) {
//jump...
}
And this work fine in most cases but only if player is giving input or last wall has a similar normal
as current so obviously the normal
isn't refreshing properly, but why?
Well, after some testing, I discovered that the OnControllerColliderHit
is called after the Update
method, and this is what I think is happening:
- Physics engine detect the collision and stops the player
- Update method not having any input just send it downwards
- The
OnControllerColliderHit
method is not triggered(this part I am sure)
Are my conclusions correct?
How can I fix this? (make the player jump without need to move the stick and just with the button)