- Home /
 
How can I stop my player from jumping off of vertical surfaces?
I have cubes in my scene, and when I jump with the player while standing next to a cube, it gets a sort of extra boost up from several jumps while traveling up the side of the cube. I would exclude it using a tag, but I want the player to still be able to jump while on top of the cube. Any suggestions? Below is a clip of the normal jump and here is a clip of what the problem is: 
Here is my Player Jumping code:
 public bool isJumping;
 public float jumpSpeed;
 
 void Start()
     {
          isJumping = false;
     }
 
 void OnCollisionStay(Collision col)
     {
         isJumping = false;
 
         rb.drag = 1f;
         speed = 1000f;
 
         if (col.gameObject.tag != "Wall")
         {
             if (!isJumping && Input.GetKey(KeyCode.Space))
             {
                 rb.AddForce(target.transform.up * jumpSpeed);
             }
         }
     }
 
 void OnCollisionExit()
     {
         isJumping = true;
         rb.drag = .1f;
         speed = 100f;
     }
 
              Answer by BuzzyRoboYT · Jan 17 at 04:24 PM
Set a groundCheck for your player that is fixed near the bottom somewhere (If you want to be able to climb up some of the walls you can add a wallCheck aswell) then do Physics.CheckSphere to check if you are touching ground and only jump when touching ground. Make sure when checking for collisions you only check in a small radius below the player and not all around.
look at this for Physics.CheckSphere: https://docs.unity3d.com/ScriptReference/Physics.CheckSphere.html
I'm not sure I understand exactly what you mean, could you write out a little bit of code for it?
do you still want me to send you some working code?
Your answer
 
             Follow this Question
Related Questions
Box collider didn't jump with player 1 Answer
Player sticks to ground occasionally after trying to jump 0 Answers
Jumping on top of an enemy problem 1 Answer
Unity2D side collision detection 1 Answer
Object Collision PLEASE HELP!!! 1 Answer