• 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 Zepeck · Mar 18, 2014 at 07:21 PM · javascriptvelocityspeedforcetorque

Im using add force to make a 'ship' go forward, if i release the key is there any way to slow it gradually?

I've got some simple code moving a 's$$anonymous$$p' around a screen. It uses AddForce() to go forward and AddTorque() to go left and right. It's all working, however if i release my forward key, the s$$anonymous$$p immediatly stops, is there any way to gradually decrease it's force or detect if i've released the key and slowly drop the force??

T$$anonymous$$s is in javascrpit just incase you didn't know.

 var moveUp : KeyCode;
 
 var turnRight : KeyCode;
 
 var turnLeft : KeyCode;
 
 var speed : int = 1;
 
 function Update () {
 
     if (Input.GetKey(moveUp))    {
         rigidbody2D.AddForce(transform.up * speed);
         Debug.Log("MoveUp");
     }
     else    {
         rigidbody2D.velocity.x = 0;
         rigidbody2D.velocity.y = 0;
         
     }
     
     if (Input.GetKey(turnRight))    {
         rigidbody2D.AddTorque(-1.2f);
     }
     if (Input.GetKey(turnLeft))    {
         rigidbody2D.AddTorque(1.2f);
     }
 
 
 }


My Code

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Mar 18, 2014 at 07:24 PM

Take out lines 15 - 19. Then select the object t$$anonymous$$s script is attached to in the Inspector, and adjust upward the 'Drag' variable in the Rigidbody component. You may also have to increase the value of 'speed' in the script above. Note that 'speed' should be a float (not an int) and it doesn't represent the speed of your object but rather the amount of force to apply. Also your code above should be in FixedUpdate() not Update().

You could instead of the above, replace these two lines:

     rigidbody2D.velocity.x = 0;
     rigidbody2D.velocity.y = 0;

With:

     rigidbody2D.velocity = rigidbody2D.velocity * 0.95;

The '0.95' will need to be adjust for how quickly you want to slow down.

Comment
Add comment · Show 1 · 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 Zepeck · Mar 18, 2014 at 08:16 PM 0
Share

Thank you very much for the help, it worked great

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

20 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

Related Questions

How to control the speed of the projectile motion? 1 Answer

Make an object move forward on its own 2 Answers

Increasing the speed of a ball in a brick breaker game 0 Answers

Clamping velocity and angular velocity at the same time? 0 Answers

How do I give my ejected bullet shell a speed plus the player's current speed? 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