- Home /
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);
}
}
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;
}
}
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!
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.
@ssshake - post this as a new question with your script. Did you check whatever value you are using is correct in the inspector?
@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
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.
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);
No one is probably going to see your post way down here but its perfect! Good answer.
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;
}
}
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.
Answer by LOL57 · Jan 11, 2016 at 06:31 AM
You probably can use this: yourTransfrom.Rotate(yourVector3*yourSpeed*Time.deltaTime);
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.
Your answer
Follow this Question
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