0
\$\begingroup\$

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:

  1. Get the wall to jump of.
  2. Check if the player pres the jump button.
  3. 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 OnControllerColliderHitis 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)

\$\endgroup\$
1
\$\begingroup\$

If you think the process is resolving backwards, then simply flip your logic. Instead of checking for the collision and setting a flag for the input to check, make the input set a flag and have the collision check for it.

\$\endgroup\$
  • \$\begingroup\$ That's a good idea but it wont work because as I said, the collision event don't trigger, and override physics collisions will cause more problems than is solves. \$\endgroup\$ – Westside Tony Jun 12 '17 at 21:45
  • \$\begingroup\$ I see. It's because OnControllerColliderHit() is only called if you explicitly called move on that frame. Does your code work fine in scenarios where you do call move? \$\endgroup\$ – Taitu Jun 12 '17 at 21:59
  • \$\begingroup\$ Yes, my code work fine the issue is for example: if i set up 2 big walls against each other so the player climb them chaining wall-jumps the control feels unresponsive as its necessary to use the stick in between jumps, It is doable, even not hard, but it doesn't feel right! \$\endgroup\$ – Westside Tony Jun 13 '17 at 9:00
  • \$\begingroup\$ Upon some investigation the function you most likely need is OnCollisionStay() which fires off every time two objects are colliding, ie. as long as the player is touching the wall. docs.unity3d.com/ScriptReference/… There is also OnCollisionEnter() and OnCollisionExit() which you may use if you need to have something occur specifically on those instances. \$\endgroup\$ – Taitu Jun 13 '17 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.