- Home /
Could someone explain me why it double jumps?
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.
Oone thing I did notice is that you are not checking what you collide with in OnCollisionStay(). Currently it doesn't matter what you collide with, it will let you jump again, maybe this is the problem? Anyway why you even have this, Physics.Linecast should be enough.
Other thing about your code is that you should always read Input in Update, not FixedUpdate. FixedUpdate is called on every physics update that is separated from framerate, which means that the is a chance where you press but it will not do anything. You can simply just move the whole if-clause from FixedUpdate to Update.
thanks $$anonymous$$ossuranta, that worked for me. Didn't see those two issues until you told me.
Answer by AmoVires · Feb 15, 2017 at 02:03 PM
Use
void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.layer == " Ground" ) grounded = true; }
OnCollisionStay triggers WHILE you are jumping , resulting in a Grounded being true. Also Kossu is right about Input.Update() is better for registering input. I do not get why are you using linecast AND OnCollision. Use one of them and check for tag/layermask.
void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "Ground")
{
grounded = true;
}
}
// P.S I'm a bit rusty.Haven't done unity in a while.Don't hold a grudge for any typos haha.
Answer by Ali-hatem · Feb 15, 2017 at 04:13 PM
grounded is handled by Physics.Linecast
so why you use
void OnCollisionStay(){
grounded = true;
}
and make sure only the ground object have a LayerMask called Ground. by the way you can declare a public LayerMask and choice from inspector better to make sure ground not set to every thing
public LayerMask WhatIsGround; void Update()
{
grounded = Physics.Linecast(transform.position, groundCheck.position,WhatIsGround);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612111115im_/https://answers.unity.com/themes/thub/images/avi.jpg)