• 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 /
This question was closed Sep 22, 2013 at 06:59 AM by Fattie for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by $$anonymous$$ · Sep 21, 2013 at 09:35 PM · jumpjava

Jump Script (Always Up)

I am using a sphere in a 3D Side-Scroller (no forward or backward) and therefore using my own controls, when I jump I can just hold it and keep going up. I need it to be like you cant jump unless on the floor. Here is my script:

 var JumpSpeed = 100.0f;
 
 function FixedUpdate() 
 {
     //Left
     if(Input.GetKey(KeyCode.LeftArrow) )
     {
         rigidbody.AddForce(Vector3.left*5);
     }
     if(Input.GetKey(KeyCode.A) )
     {
         rigidbody.AddForce(Vector3.left*5);
     }
     //Right
     if(Input.GetKey(KeyCode.RightArrow))
     {
         rigidbody.AddForce(Vector3.right*5);
     }
     if(Input.GetKey(KeyCode.D))
     {
         rigidbody.AddForce(Vector3.right*5);
     }
     //Jump
     
     if(Input.GetKey(KeyCode.W))
     {
         Jump();
     }
 }
 function Jump()
 {
         animation.Play("jump_pose");
         rigidbody.velocity += Vector3.up * JumpSpeed;
 }
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 meat5000 ♦ · Sep 21, 2013 at 09:37 PM 0
Share
 rigidbody.velocity += Vector3.up * JumpSpeed;

Just change that line so you don't always go up. $$anonymous$$aybe change forcemode to impulse and apply it only once on an AddForce rather than changing velocity.

avatar image $$anonymous$$ · Sep 21, 2013 at 09:38 PM 0
Share

Would you $$anonymous$$d showing me how to do this?

avatar image mattssonon · Sep 22, 2013 at 12:03 AM 0
Share

Are you using a CharacterController?

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by meat5000 · Sep 21, 2013 at 10:52 PM

For an object with rigidbody

     if(Input.GetKeyDown(KeyCode.W))
     {
         Jump();
     }
  }
         
  function Jump()
  {
     animation.Play("jump_pose");
     rigidbody.AddForce(Vector3.up * JumpSpeed, ForceMode.Impulse);
  }
Comment
Add comment · Show 4 · 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 mattssonon · Sep 22, 2013 at 12:02 AM 0
Share

Won't this just make the ball keep moving upwards if the player keeps tapping W?

avatar image meat5000 ♦ · Sep 22, 2013 at 12:05 AM 0
Share

Yup. Its a nudge in the right direction :)

If people want to make games they need to work things out. If we spell it out, people won't learn anything!

avatar image $$anonymous$$ · Sep 22, 2013 at 12:16 PM 0
Share

Yes but that is the problem, I can't use a character controller because im using a sphere. That means no isGrounded.

avatar image meat5000 ♦ · Sep 22, 2013 at 01:14 PM 0
Share
 if ( rigidbody.velocity.y == 0 )
 
 if ( rigidbody.velocity.y != 0 )

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

16 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

Related Questions

script for Jump help 3 Answers

Javascript jumping touchpad. 1 Answer

The player ball must jump immediately - 1 Answer

Simple wall jump 0 Answers

Can anyone understand the problem with this script? 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