Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by skiedude · Aug 15, 2016 at 06:05 PM · jumpwall

Disable being able to jump up a wall

Its my 2nd day with Unity, just finished the Roll'o ball game tutorial. I decided I wanted to make the game more unique and started adding some features. One I added was the ability to jump. This works great except when I'm up next to a wall. If I hit the wall at speed and spam spacebar, I can at time successfully continue to jump up the wall and over. I found http://answers.unity3d.com/questions/420946/why-is-my-jump-height-affected-by-walls.html which reduced the cases of being able to jump up the wall, but I'm still able to in some cases.

Any tips on getting rid of the ability to jump higher against walls all together?

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour
 {
     public float speed;
     public Text countText;
     public Text winText;
     
     private Rigidbody rb;
     private int count;
     public bool grounded = true;
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         count = 0;
         SetCountText();
         winText.text = "";
     }
     void FixedUpdate()
     {
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
 
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
         rb.AddForce(movement * speed);
 
         if(Input.GetKeyDown (KeyCode.Space))
         {
             Jump();
         }
     }
 
     void Jump()
     {
         if(grounded == true)
         {
             rb.AddForce(new Vector3(0, 4, 0), ForceMode.Impulse);
             grounded = false;
         }
     }
 
     void OnCollisionEnter (Collision hit)
     {
         grounded = true;
     }
 
     void OnTriggerEnter(Collider other)
     {
         if(other.gameObject.CompareTag("Pick Up"))
         {
             other.gameObject.SetActive(false);
             count = count + 1;
             SetCountText();
         }
     }
 
     void SetCountText()
     {
         countText.text = "Count: " + count.ToString();
         if(count >= 10)
         {
             winText.text = "YOU WIN!";
         }
     }
 
 }
 

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by skiedude · Aug 15, 2016 at 03:53 PM

After toying with it the rest of the weekend I did find one resolution that from my testing appears to work in all cases I had before.

I added a tag to all my walls, and added a case in the OnColliionEnter

      void OnCollisionEnter (Collision hit)
      {
          if(hit.gameObject.CompareTag("Wall"))
          {
             grounded = false;
          }
          else
          {
          grounded = true;
          }
      }

Comment
Add comment · Show 2 · 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
avatar image Redjar · Mar 22, 2017 at 07:02 PM 0
Share

Great idea, thanks!

avatar image Sophia667 Redjar · Sep 07, 2018 at 06:52 AM 0
Share

Thank you so much for the hint! $$anonymous$$ay I ask what do you mean "added a case in the OncollionEnter"? I followed your code and added tag to all of my walls, but the players are still jump up the wall and fall off...

avatar image
2

Answer by avishaishai · May 23, 2019 at 06:50 PM

Hi, I saw some suggested to use friction or drag and other methods within the rigid body, but I figured out it can be done by checking the hitPos on collision, so you can say which side of the platform was collided. Here is my suggestion, hope this will help someone.

  private void OnCollisionStay2D(Collision2D collision)
 {
     foreach (ContactPoint2D hitPos in collision.contacts)
     {
         if (hitPos.normal.x != 0) // check if the wall collided on the sides
             isGrounded = false; // boolean to prevent player from being able to jump
         else if (hitPos.normal.y > 0) // check if its collided on top 
         {
             isGrounded = true;
             print("grounded");
         }
         else isGrounded = false;
     }
 }
Comment
Add comment · Show 4 · 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
avatar image ALBI04 · Aug 24, 2019 at 10:54 AM 0
Share

HI, i tried your method but it's still not working the player get still stuck in walls like this https://youtu.be/OebWZJ-x6AQ

how do i fix it. I'm using tileset for level design and a box collider and rigidbody for the player

avatar image avishaishai ALBI04 · Aug 25, 2019 at 07:10 AM 0
Share

Hi, try to check hitPos.normal.x values on the console when player collides, and adjust the condition based on it. $$anonymous$$aybe it has to get a range of results, you can try this one -

                 if (hitPos.normal.x > 0.9 && hitPos.normal.x < 1.1 || hitPos.normal.x < -0.9 && hitPos.normal.x > -1.1)
                 {
                     isGrounded = false;
                 }


avatar image gameCoder95 · Jun 14, 2020 at 03:39 AM 0
Share

Thank you so much! This solved my exact problem!

avatar image nailuj29gaming · Aug 28, 2020 at 09:28 PM 0
Share

It might also help to return from the message if the player is grounded, otherwise you can't jump when you are against a wall

avatar image
0

Answer by BetaAnalysis · Aug 16, 2016 at 04:59 PM

what I do is have a small box collider on the bottom of the char, and a big one on the top (if youre looking from top down view the top one should be bigger than the bottom)

then only run the code for the bottom collider, thus when you touch a wall, the top collider is hitting not the bottom one(which is used for the isGrounded)

Comment
Add comment · Show 2 · 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
avatar image Yahtzebra · Jan 10, 2018 at 07:25 PM 0
Share

How did you only run code for one collider?? Sorry I know this is old but no one seems to know how to do this.

avatar image sevic69 Yahtzebra · Feb 10, 2018 at 09:30 PM 0
Share

@Yahtzebra I think he means he has an empty GameObject on the players feet, and that empty GameObject has a Collider and a script to detect the collision.

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

12 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

Related Questions

How do I disable player from jumping up the wall? 0 Answers

unity 2d wall jump 0 Answers

how do I make my character jump while running without losing all your speed 1 Answer

Unrealistic jump in C# 1 Answer

How to play attack animation multiple times with mechanim 0 Answers


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