Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 dhvl5 · Sep 20, 2018 at 07:30 AM · objectgamerigidbody2dspeedconstant

how to keep speed canstant in flappy bird?

I'm creating flappy bird for learning and when I play bird's speed is constantly increasing somehow, How can I fix that? I've used this code : rb2d.AddForce(new Vector2(speed, 0));

Comment
Add comment · Show 1
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 Casiell · Sep 20, 2018 at 09:35 AM 0
Share

Are you using this in update? Because if that's the case then you are adding force each frame resulting in constant speed increase

2 Replies

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

Answer by ModLunar · Sep 20, 2018 at 11:13 AM

Also, if you wanted to use force to do it, in theory, it should work if you calculate the required force each frame to get back to the velocity you want, like this:

Using these 2 equations from physics:

//Newton's Second Law

F = ma

//Constant acceleration (for a very short amount of time -- during FixedUpdate for example)

a = Δv/t


(Substitute Δv/t for a, into the equation F = ma)

F = mΔv / t



So therefore, as an example, if you're applying this force to the Rigidbody2D in a MonoBehaviour's FixedUpdate(), it might look like this:

 using UnityEngine;
 
 public class YourFlappyBirdScript : MonoBehaviour {
     [SerializeField] private Vector2 targetVelocity;
     private new Rigidbody2D rigidbody;
 
     public void FixedUpdate() {
         //F = mΔv / t
         Vector2 force = rigidbody.mass * (targetVelocity - rigidbody.velocity) / Time.fixedDeltaTime;
         rigidbody.AddForce(force);
     }
 }

Please don't use that name though for the script, find a better name haha XD. Anyway, notice that for Δv, it's whatever your "target" velocity is (must be a vector), subtracted by the Rigidbody2D's current velocity. This is the different from your current velocity to the one you want to get to -- that's exactly the velocity difference you want to change, to get to the targetVelocity.

If you're using speed, (which that is often times more convenient), just multiple your speed (a float, scalar) with a unit vector (Vector2) for its direction. This will get you your targetVelocity.




And to be honest, I hope this works. Because usually when I inject formulas into the physics engine, it's often times off for some mysterious reason that refuses to show itself! If it doesn't, then you can directly set the velocity. (Note that in 3D, Unity recommends not directly setting Rigidbody's velocity. Instead, use AddForce(...) with ForceMode.VelocityChange.)

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
avatar image
0

Answer by SkaredCreations · Sep 20, 2018 at 09:51 AM

Instead of adding force it'll be easier if you update directly the velocity of the rigidbody.

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

131 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Slow down Rigidbody's speed to a stop 2 Answers

Why do my objects keep changing speed? 1 Answer

Pushing and object up/down/left/right at constant speed 0 Answers

My moving object decelerates when almost reaching a next point in the bezier curve,My gameobject decelerates when reaching a next point 0 Answers

How to get the speed of an object? 3 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