- 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 MSavioti · Jan 06, 2021 at 10:51 PM
Don't use OnCollision, use Raycasting.
Say you you'll only wall jump if you're facing the wall. In this case, Cast some rays to detect a wall in front of your character. If it detected a wall and it's not grounded, apply the jump force to the desired direction.
I'd also recommend short rays cast from your character feet to detect ground.
Your answer
Follow this Question
Related Questions
How do I make a Rigidbody 3D wall jump? 1 Answer
3D wall jump almost working 1 Answer
I'm trying to make a 3D player do a wall jump. Does anyone have advice? 0 Answers
3D box collider go through terrain 1 Answer
Shake Effect 0 Answers