- Home /
How to detect if characterController is facing a wall?
I'm working on a wall jump, and it works fine, except for the fact that the player can wall jump at any rotation. I was wondering if I could detect if the player is facing the wall within a certain angle.(this is a 3rd person view game)
Here is a rough estimate of my code:
private void OnControllerColliderHit(ControllerColliderHit hit) {
if (!controller.isGrounded && hit.normal.y < 0.1f && facingWall) {//facingWall is not a real variable. it is a placeholder for supposively where I would check if the player is facing the wall within a certain angle.
//Debug.DrawRay(hit.point, hit.normal, Color.red, 1.25f);
if (Input.GetButtonDown("Jump")) {Jump();}
}
}
Answer by dtrevillyan · Mar 24, 2019 at 12:35 AM
@Vakore, you could test the y component of the hit normal vector however it is very difficult to determine direction using that. I recommend that you calculate the dot product of the forward vector for the controller gameobject (transform.forward) and the hit normal vector. If you aren't familiar with the dot product it is the length of the two vectors and the cosine of the angle between the two vectors. Since both are unit length (length = 1) then the dot product is the cosine of the angle. If the dot product is zero then the controller gameobject is facing along the wall (wall is to the left or right), if it is 1 then the wall is behind and if it is -1 then the controller is facing the wall. So, if your dot product is acceptably close to -1 then you are facing the wall. Hope that helps.
Your answer
Follow this Question
Related Questions
OnControllerColliderHit Doesn't always get detected? 0 Answers
ControllerColliderHit.normal is different when hit.gameObect scale is bigger. why? 0 Answers
Detecting collisions with OnControllerColliderHit when not moving 0 Answers
How to make a CharacterController know it hits another 1 Answer
OnControllerColliderHit Problem ! 1 Answer