• 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
Question by lmarvelly · Mar 10, 2015 at 09:51 AM · controllerjumpjumpingcontrolscontrol

Jumping while moving

Hi I'm fairly new to writing script with Unity. I'm trying to get a ball to jump more than once without stopping. The ball will jump just fine while moving left or right but has to stop before it can jump again. Can anybody suggest how I can fix this problem?

 #pragma strict
 
 var Jump : KeyCode;
 var stop : KeyCode;
 var moveLeft : KeyCode;
 var moveRight : KeyCode;
 
 var speed : float = 1;
 
 var jumpHeight : float = 250;
 var grounded = false;
 
 function Update ()
 {    
     if (Input.GetKey(moveLeft))
       {
           GetComponent.<Rigidbody2D>().velocity.x = speed * -1;
           if (Input.GetKey(Jump))
         {
             jump();
         }
       }
     else if (Input.GetKey(moveRight))
       {
         GetComponent.<Rigidbody2D>().velocity.x = speed;
         if (Input.GetKey(Jump))
         {
             jump();
         }
         
       }
     else if (Input.GetKey(stop))
       {
           grounded = true;
         GetComponent.<Rigidbody2D>().velocity.y = 0;
       }
     else if (Input.GetKey(Jump))
         {
             jump();
         }
     else 
       {
           grounded = true;
         GetComponent.<Rigidbody2D>().velocity.x = 0; 
       }
 }
 
 function OnCollisionEnter(hit : Collision)
     {
         grounded = true;
     }
 
 function jump()
 {
     if (grounded == true) 
     {
         GetComponent.<Rigidbody2D>().AddForce(Vector3.up * jumpHeight);
         grounded = false;
     }
 }
 
 
Comment

People who like this

0 Show 3
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 lmarvelly · Mar 10, 2015 at 11:47 PM 0
Share

Cool thanks a lot. Now the only problem I have is when I hold down the jump button down the ball floats upwards...

avatar image ForeignGod · Mar 11, 2015 at 05:19 PM 0
Share

To do this simply replace GetKey with GetKeyDown

Make sure to mark you question as answered ^^

avatar image lmarvelly · Mar 17, 2015 at 06:03 PM 0
Share

Ok thank you :)

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by ForeignGod · Mar 10, 2015 at 01:54 PM

  var RSpeed = GetComponent.<Rigidbody2D>().velocity.y;
 
  function Update ()
  {
      if (RSpeed == 0)
         {
         grounded = true;
         }
  }

Adding this to the script should check if you have any velocity on the Y axis, if the velocity is 0 grounded is true. This might fix why grounded wont become true when you keep on moving. I haven't tested this so there might be errors.

EDIT: I tested the script and if i walk left/, jump and land, grounded is true. This should fix your problem ^^

Comment
lmarvelly

People who like this

1 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

fps jump Brackeys 0 Answers

how can I make the character jump more by holding the jump button in this script? 1 Answer

How to make camera position relative to a specific target. 1 Answer

On a VR headset, the player jumps too many times sometimes when pressing a controller button. 1 Answer

How do I make this jump function work? 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