- Home /
How do I make a Rigidbody 3D wall jump?
So I've made a simple third person parkour script, and want to implement wall jumping, and I'm not sure how to go about doing it. Here's how I detect wall collision:
void OnCollisionEnter(Collision collision)
foreach (ContactPoint contact in collision.contacts)
{
if (!onGround && rb.position.y > -16f)
{
Debug.DrawRay(contact.point, contact.normal, Color.red, 10f);
}
}
And heres my movement script:
void Update() { if (Input.GetKeyDown(KeyCode.Space) && onGround == true) { speed = jumpSpeed; onGround = false; rb.AddForce(jump * jumpForce, ForceMode.Impulse); } }
void FixedUpdate()
{
moving = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D);
if (moving)
{
transform.rotation = Quaternion.Euler(0, Camera.rotation.eulerAngles.y, 0);
}
float x = Input.GetAxisRaw("Horizontal");
float y = Input.GetAxisRaw("Vertical");
Vector3 move = (transform.right * x + transform.forward * y);
move.Normalize();
rb.velocity = new Vector3(move.x * speed * Time.deltaTime, rb.velocity.y, move.z * speed * Time.deltaTime);
onGround = Physics.CheckSphere(new Vector3(transform.position.x, transform.position.y - 1, transform.position.z), spSize, Ground);
}
Any answers would be greatly appreciated, sorry if this is a stupid question I'm pretty new to Unity.
Answer by Llama_w_2Ls · Jan 08, 2021 at 09:56 PM
The way I implemented wall jumping was using a rigidbody controller, which I can see you're also using.
Wall Jumping
Detecting Walls
So the way I detected if the player is near a wall, and able to wall-jump, was by using a separate trigger collider, slightly larger than the player's collider. The script would run as follows:
private void OnTriggerStay(Collider other)
{
if (!IsGrounded)
IsNearWall = true;
else
IsNearWall = false;
}
private void OnTriggerExit(Collider other)
{
IsNearWall = false;
}
This uses the OnTriggerStay
method, which would detect if a wall enters the player's trigger collider, and while it is in the trigger, allow wall jumping, through the bool IsNearWall
. There is also a check for ground, because if the player is stationary and near a wall, you wouldn't want them to wall jump.
How To Jump Off Walls
Jumping off walls work by adding a force to the rigidbody, in the direction of the wall's normal. This is the direction that the wall is facing, essentially. You can get this, using a raycast, which is what the code below does.
private void WallJumpCheck()
{
// If jump button is pressed on a wall
if (Input.GetKeyDown(KeyCode.Space) && IsNearWall)
{
// Get the forward, backward, left and right directions of the player
// NOTE: some directions not necessary if your player can only jump on
// some of the directions in the array
Vector3[] Directions = new Vector3[]
{
transform.forward,
transform.forward,
transform.right,
-transform.right
};
foreach (Vector3 direction in Directions)
{
// Fire a small raycast out of the player to the wall in the given direction
if (Physics.Raycast(transform.position, direction, out RaycastHit hit, 1f))
{
// Add a force to the player in the direction of the wall's normal
rb.AddForce(hit.normal * JumpForce * Time.deltaTime, ForceMode.Impulse);
break;
}
}
}
}
There's a lot of code comments in there, so it should be clear how it works. Just a simple raycast in the player's sides and forward directions, trying to find the wall's normal, and adding force.
NOTE: This would be problematic, if the player is in a corner, where there are two walls, and the player might not know which wall normal to get.
I hope this gives you some insight into one way of implementing a wall jump. I would love to see how far you get! @RandumbGuy