Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RandumbGuy · Jan 08, 2021 at 06:41 PM · rigidbodywall collisionwall jumpwalljump

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

156 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make a rigidbody 3D wall jump? 1 Answer

Third Person Player stuck on wall 0 Answers

3D wall jump almost working 1 Answer

Wall jump push force 1 Answer

Why isn't my player looking the other direction when I wall jump (please help I am desperate) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges