Hello, I couldn’t find anyone else with the same issue so I am posting it.
I followed the Creating a Basic Platformer Game tutorial from unity. With their code I was applied some of it into another code, but for 3D.
The code is about the property of being only able to jump once. In my code, the player can jump twice for some reason.
With this code I am also getting the error "NullReferenceExceptionL Object reference not set to an instance of an object. Line 26. Which would be the only line inside of grounded.
public float speed = 5.0f;
public bool facingRight = true;
//public float maxSpeed = 10.0f;
//public bool jump = false;
public float jumpForce = 100f;
private Transform groundCheck; // check the ground/ceilling/ or anything we might use - might be useful later
private bool grounded = false; // touched the ground/ceilling/ or anything we might use
private Rigidbody rb;
// Use this for initialization
void Start () {
groundCheck = transform.Find("groundCheck");
rb = GetComponent<Rigidbody>();
}
void Update()
{
grounded = Physics.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
}
void OnCollisionStay()
{
grounded = true;
}
// Update is called once per frame
void FixedUpdate () {
float moveHorizontal= Input.GetAxis("Horizontal");
Vector3 movement = new Vector3(moveHorizontal, 0);
rb.AddForce(movement * speed);
if (Input.GetKeyDown(KeyCode.Space) && grounded)
{
rb.AddForce(new Vector3(0, jumpForce));
grounded = false;
}
}
Could someone explain me why I am double jumping? Thanks in advance and sorry if there was actually someone who had the same issue.