Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
7
Question by cj31387 · Jun 12, 2012 at 07:40 AM · physicsvelocityspeedmovelimit

Limiting rigidbody Speed.

Hi, me and another person have been making a breakout clone. We used unity rigidbody real physics to do the ball movement, but the ball always ends up going way to fast over time. We are very new to programming and would like to just finish the game already. Could someone help us by showing us how you would limit the speed of a ridgidbody. Currently when the ball hits the paddle oncollision we have a addforce(x,y,z) also same thing when it hits a wall or brick. but yeah after about 30 seconds its going way to fast to play. We want to limit the speed at a certain speed. Thanks hope someone can help.

 using UnityEngine;
 using System.Collections;
 
 public class Ball_Movement : MonoBehaviour {
     public float count = 0;
     public static bool FireBall=false;
     public int timeOfBonus = 10;
     public static Ball_Movement instance;
     public static bool playerDead = false;
 
     
     void Awake()
     {
         instance=this;
           transform.parent = null;
     }
         
     // Update is called once per frame
     
     // When space is pressed the ball launches and breaks free from the parent.
     void FixedUpdate() {
         if (Input.GetKey ("space"))
         {    
           count++;
                 AddForce();
                              
         }
         // bounces from the wall
         if (gameObject.rigidbody.collider.CompareTag("wall"))
         {
             rigidbody.AddForce(-1500, 0, -1500);
         }
                                       
     }
 // it is adding score to score not to global Score just change it according to your pref but be carefull xD
     void OnCollisionEnter(Collision block)
     {
         if (block.collider.CompareTag("block10point"))
         {
             ScoreBoard.Score = ScoreBoard.Score + 10f;
 
         }
         if (block.collider.CompareTag("block20point"))
         {
            ScoreBoard.Score =ScoreBoard.Score + 20f;
 
         }
         if (block.collider.CompareTag("loosingwall"))
         {
             BallCounter.ballCount = BallCounter.ballCount - 1f;
             Destroy(gameObject);
             if (BallCounter.ballCount <= 0)
             {
                 BallCounter.ballCount = 1;
                 ScoreBoard.Lives = ScoreBoard.Lives - 1;               
                 playerDead = true;
                 Destroy(PaddleMove.Instance.gameObject);
                 
             }
                                                     
         }
          
     }
 
 
     void AddForce()
     { 
         if (count < 2) 
        rigidbody.AddForce(1500, 0, 1500);
             else
             return;
   }
 public void AddForce2()
     {
         rigidbody.AddForce(100, 0,100);
     }
     
 }
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

5 Replies

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

Answer by whydoidoit · Jun 12, 2012 at 08:06 AM

How about adding a script which does this:

   public float maxSpeed = 200f;//Replace with your max speed

   void FixedUpdate()
   {
         if(rigidbody.velocity.magnitude > maxSpeed)
         {
                rigidbody.velocity = rigidbody.velocity.normalized * maxSpeed;
         }
   }
Comment
Add comment · Show 14 · 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 cj31387 · Jun 12, 2012 at 08:15 AM 0
Share

wow Ive been searching for a answer for like a week and never came close to limiting the speed, this totally works, Thanks a ton!

avatar image ssshake · Jul 25, 2013 at 03:08 PM 0
Share

I find this doesnt work for my at all, any value I set causes the ball to crawl. Also a debug.log in this block of code doesnt print anything. $$anonymous$$ind of confused.

avatar image robertbu · Jul 25, 2013 at 03:10 PM 0
Share

@ssshake - post this as a new question with your script. Did you check whatever value you are using is correct in the inspector?

avatar image calebgray · Feb 09, 2015 at 05:46 PM 1
Share

@nicloay: you're correct... simply checking "greater than max speed" then "set to max speed" will cause objects that are moving significantly faster to appear to "jerk." To prevent the "jerk" from happening, you'd have to have some kind of "Near$$anonymous$$axSpeed" threshold where you could slow the object down over time until it actually reaches $$anonymous$$axSpeed. This concept is known as "damping:" http://www.box2d.org/forum/viewtopic.php?p=13255&sid=35e66dc8183ee9a9424bfa2dc7c4c5e9#p13255

avatar image Raunchard · Nov 07, 2015 at 12:19 AM 3
Share

How about No? changing the velocity of a rigid body directly should NEVER be done on a constant basis. This breaks the physics engine, and makes little baby Jesus cry.

Show more comments
avatar image
27

Answer by TechTheAwesome · Sep 18, 2017 at 02:12 PM

a shorter way to do this probably using Vector3.ClampMagnitude @whydoidoit

 public float maxSpeed;
 
 rigidbody.velocity = Vector3.ClampMagnitude(rigidbody.velocity, maxSpeed);



Comment
Add comment · Show 3 · 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 MistaGoustan · Oct 13, 2017 at 05:42 AM 0
Share

No one is probably going to see your post way down here but its perfect! Good answer.

avatar image Legendmir · Dec 17, 2017 at 01:53 PM 0
Share

Worked like a charm for me

avatar image NT_Ninetails · Nov 20, 2020 at 01:39 AM 0
Share

I experienced the jerk with whydoidoit's answer and the solution link is broken. I used this and it worked so easily it hurts (I spent all morning on this). Here's my code for reference.

 // my rolling ball
             if (movementType == AEntityClass.$$anonymous$$ovementType.Roll)
             {
                 float3 requestSpeed = (requested$$anonymous$$ovementDirection * ccComponentData.$$anonymous$$ovementSpeed) * (ccInternalData.SupportedState != CharacterSupportState.Supported ? 0.1f : 1f);
                 float3 realVelocityUp = realVelocity.Linear * up;
                 float3 v = Vector3.Clamp$$anonymous$$agnitude(requestSpeed + (realVelocity.Linear-realVelocityUp), ccComponentData.$$anonymous$$ax$$anonymous$$ovementSpeed) + (Vector3)realVelocityUp +
                     (shouldJump ? (Vector3)ccInternalData.UnsupportedVelocity : Vector3.zero);
                 
                 desiredVelocity.Linear = v;
             
                 // deal with damping!
                 if (ccInternalData.SupportedState != CharacterSupportState.Supported)
                 {
                     ccInternalData.Damping = damping;
                     damping = AEntityClass.DefaultPhysicsDamping; // linear = 0.01, Angular = 0.05
                 }
                 else if (!damping.Equals(ccInternalData.Damping))
                 {
                     damping = ccInternalData.Damping;
                 }
             }

avatar image
1

Answer by nicloay · Feb 09, 2015 at 06:09 PM

@calebgray Thanks for reply (sorry i've acidenlty removed my previous comment) One of solution which i found is (setup proper air resistance (Linear drag), so it would limit the max speed for your rigid body) Then to fix your gravity, you would need to change gravity scale. Unfortunately i don't have any formulas, so i've seed up this parameter empirically.

After all of this changes, physics movement works very smooth and nice.

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 LOL57 · Jan 11, 2016 at 06:31 AM

You probably can use this: yourTransfrom.Rotate(yourVector3*yourSpeed*Time.deltaTime);

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 Animladen · Mar 03 at 10:47 AM

How do you limit Rigidbody on one specific direction. So that i can cap the speed individual axes.

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

23 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

Related Questions

Constraining maximum movement speed on a rigidbody on certain axes. 1 Answer

Measure speed in single axis 1 Answer

Camera movement speeds up when restarting game using Application.LoadLevel? 0 Answers

How to stop objects from losing energy due to joints? 0 Answers

Add velocity relative to ground? 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