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 Why is my Jump Height affected by Walls? - Unity Answers 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!";
}
}
}