Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 'ship' 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 ship 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??

This 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 this 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.

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

Rigidbody - Applying One-Time Force? Not Constant Force 2 Answers

Can I, someway, avoid centripetal force? 0 Answers

How to get collision force, not relative velocity? 1 Answer

Using rigidbody.AddForce and speeding the object 2 Answers

i am not able to increase the speed of ball perodically in pong, in this code the speed of ball decrease while playing so help with this, how to increase speed of ball perodically?, 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