• 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 waterbottleman142 · Feb 01, 2021 at 10:12 PM · gamecodepage

how to limit jumping

so im new to coding and im making a game and something has been annoying me the past few days is that if i press the jump button over and over he just keeps going up here is the code its a 2d platformer please help {

 //Movement
 public float speed;
 public float jump;
 float moveVelocity;
 //Grounded Vars
 bool grounded = true;
 void Update()
 {
     //Jumping
     if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Z) || Input.GetKeyDown(KeyCode.W))
     {
         if (grounded)
         {
             GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jump);
         }
         
     }

     moveVelocity = 0;

     //Left Right Movement
     if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
     {
         moveVelocity = -speed;
     }
     if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
     {
         moveVelocity = speed;
     }

     GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, GetComponent<Rigidbody2D>().velocity.y);

 }
 //Check if Grounded
 void OnTriggerEnter2D()
 {
     grounded = true;
 }
 void OnTriggerExit2D()
 {
     grounded = false;
 }

},so im new to coding and i have been trying to make a game and for a few days now i have been stuck on trying to make him only jump once here is my code please help {

 //Movement
 public float speed;
 public float jump;
 float moveVelocity;
 public float maxjumps;
 public float jumps;
 //Grounded Vars
 bool grounded = true;
 void Update()
 {
     //Jumping
     if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Z) || Input.GetKeyDown(KeyCode.W))
     {
         if (grounded)
         {
             GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jump);
         }
         
     }

     moveVelocity = 0;

     //Left Right Movement
     if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
     {
         moveVelocity = -speed;
     }
     if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
     {
         moveVelocity = speed;
     }

     GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, GetComponent<Rigidbody2D>().velocity.y);

 }
 //Check if Grounded
 void OnTriggerEnter2D()
 {
     grounded = true;
 }
 void OnTriggerExit2D()
 {
     grounded = false;
 }

}

Comment
Add comment · Show 1
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 imalexrd · Feb 02, 2021 at 12:13 AM 0
Share

Check with Debug.Log() if the exit trigger is being called

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by logicandchaos · Feb 02, 2021 at 02:03 AM

it looks like it's all coded right, I think the issue is with

  //Check if Grounded
  void OnTriggerEnter2D()
  {
      grounded = true;
  }
  void OnTriggerExit2D()
  {
      grounded = false;
  }

I have found that approach to be unreliable, it should work but doesn't seem to work consistently. There are many other ways to check if a character is grounded, an easy way if you are using unity built in gravity is to check it's y velocity, if it's not moving up or down, it should be on solid ground. So you could do bool isGrounded = rb.velocity.y < .1f && rb.velocity.y > -.1f; but there are a lot of other ways, check out this video:

https://www.youtube.com/watch?v=c3iEl5AwUF8&t=822s

there are 3 ways and if you check the comments I add in another.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

138 People are following this question.

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

Related Questions

How to make a game start when i click mouse 1 Answer

getting udp package info inside unity (GlovePIE) 0 Answers

Pressing "Fire2" Spawns and removes object on click 0 Answers

How to make a map/Open Terrain 1 Answer

How do i create a game like Subway Surf (Infinite terrain)? 0 Answers


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