• 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 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
Add comment · 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 Get$$anonymous$$ey with Get$$anonymous$$eyDown

$$anonymous$$ake sure to mark you question as answered ^^

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

Ok thank you :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
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
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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

fps jump Brackeys 0 Answers

Hey everyone. I need some help please. Does any of you know a Bike controller/physics tutorial? ,does anyone know any tutorials on bike movement and Physics? 2 Answers

Jump Further When Key Is Held Down 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