- Home /
Jumping away from a wall
I am trying to make a 2D platformer and I want the player to jump away from the wall when he does a wall jump. I recall during an FPS tutorial I did that there was some API function that allowed you to find the angle perpendicular to the surface you had collided with.
For instance, if you were to shoot the ground from any angle, it knew to animate the spark straight up away from the ground. Also, if you shot the wall, it knew to animate sideways away from the wall.
That FPS tutorial is not longer there so I'm not sure if that was one of the outdated functions. Any help will be greatly appreciated. I am using the character controller.
if (controller.collisionFlags == CollisionFlags.Sides) //if touching walls only
{
moveDirection.x = //away from the wall;
moveDirection.y = wallJumpSpeed;
}
Does anyone know if I could use Vector3.Reflect() or Quaternion.FromToRotation()? Or maybe I could check to see if one side is free and then jump towards that direction?
Answer by Tyrrrz · Sep 28, 2013 at 08:55 PM
You can use character controller's OnControlColliderHit event and analyze the ControllerCollidrHit argument which has a "normal" property which returns collision normal vector and "moveDirection" which returns the moving direction in the moment of collision. Then you can use movement = Vector3.Reflect(moveDirection, normal); and also add some vertical speed to keep afloat for chain wall jymps movement.y += jumpOffHeight;
Doesn't Vector3.Reflect() require continuous movement? I was wanting a brief cling when the player hit the wall.
Also, once I learned about the physics limitations of the character controller I decided to use a rigid body controller. This is what I've been able to find to achieve what I wanted.
void OnCollisionStay(Contacts contact)
{
if(Vector3.Angle(contact[0].normal, Vector3.Right) == 180)
{
//right wall
}
else if (Vector3.Angle(contact[0].normal, Vector3.Left == 180)
{
//left wall
}
}
Let me know if you know of any better way of doing this.
Small correction: it's the OnControllerColliderHit
event. (Controller ins$$anonymous$$d of Control).
Your answer
Follow this Question
Related Questions
3d wall jumping with charter controller 1 Answer
HELP! stuck on wall jump Unity 2D 1 Answer
Jump and Hold on!! 0 Answers
Jumping away from the wall 0 Answers
Trying to get my wall Jump to Work 1 Answer