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 keshk · Apr 12, 2015 at 12:48 AM · unity 5

Addforce increases the object's speed overtime

I am trying to addforce to an object to let it constantly move to the right on a fixed speed. But when I try it with the following code, the speed starts slowly and starts adding up and going very fast over time. How can I keep the speed to be constant without adding on.

I do not want it to start slowly. I want it to immediately react to the speed and keep to the same speed through out.

I am trying to use AddForce instead of switching to manipulating velocity directly. Please advice what I am doing wrong.

 public float speed = 15;
 Rigidbody2D rb;

 void Start()
 {
     rb = GetComponent<Rigidbody2D>();
 }

 void Update()
 {
     rb.AddForce(Vector2.right * speed * Time.deltaTime);
 }

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

2 Replies

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

Answer by CodeElemental · Apr 12, 2015 at 12:51 AM

You could use

 rb.velocity = new Vector2(direction * speed * Time.deltaTime, rb.velocity.y);

Comment
Add comment · Show 6 · 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 keshk · Apr 12, 2015 at 01:06 AM 0
Share

I have another AddForce which controls the jump as follows. Directly adding velocity this way going to affect that portion.

rb.AddForce(Vector2.up * jumpSpeed);

avatar image CodeElemental · Apr 12, 2015 at 01:22 AM 0
Share

Then you need to preserve the Y component of the velocity and only affect the horizontal one.

 direction = 1; // for left put -1;
 rb.velocity = new Vector2(direction * speed * Time.deltaTime, rb.velocity.y);
avatar image keshk · Apr 12, 2015 at 01:37 AM 0
Share

New vector2 only takes in no parameters or 2 floats which are essentially the x and y numbers. How are you adding that velocity.y at the end. Is it a variable you declared? Cos its says to me "'velocity' does not exist in current context".

avatar image CodeElemental · Apr 12, 2015 at 01:56 AM 0
Share

Fixed it, sorry. The Y component should be the original one (from the rigidbody object).

avatar image CodeElemental · Apr 12, 2015 at 03:33 PM 0
Share

Did you resolve the problem ?

Show more comments
avatar image
0

Answer by Limesta · Apr 12, 2015 at 05:15 PM

Something you can attempt is to give your object a jolt of speed at the beginning and then add force over time afterwards I would do this_>

 if (rb.velocity < 10)
 {rb.AddForce(Vector2.right * speed, forcemode2d.Impulse);}
 else
 {
     rb.AddForce(Vector2.right * speed * Time.deltaTime);
 }

that 20 can be changed to whatever you want your threshold to be.

So if your velocity is under 10, give it a large speed boost, after that just add a minor amount of force

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.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

How to set multiple android name in one android manifest?? 0 Answers

Automatic Network Migration Manager 0 Answers

Android build misbehaving. 1 Answer

Incorrect velocity after collision 1 Answer

Can I publish my game with Unity 5.5 preview? 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