Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Ariete · Feb 15, 2017 at 07:36 AM · nullreferenceexceptiongroundeddouble jump

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.

Comment
Add comment · Show 2
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 Kossuranta · Feb 15, 2017 at 12:41 PM 1
Share

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.

avatar image Ariete Kossuranta · Feb 15, 2017 at 09:18 PM 0
Share

thanks $$anonymous$$ossuranta, that worked for me. Didn't see those two issues until you told me.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.
 


Comment
Add comment · 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
0

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);
  }
Comment
Add comment · 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

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

problems with grounded and double jumping 2 Answers

NullReferenceException...Camera problem??? 1 Answer

NullReferenceException: Object reference not set to an instance of an object 1 Answer

Help On error Please 1 Answer

For Loop and Null referance error Ios Scripting 1 Answer


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