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!";
}
}
}
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;
}
}
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...
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; } }
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
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;
}
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
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)
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.
@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
Follow this Question
Related Questions
How do I disable player from jumping up the wall? 0 Answers
unity 2d wall jump 0 Answers
Make a rigidbody Jump (global up) 3 Answers
Multi-touch buttons 0 Answers