- Home /
Limit Speed One Axis
Hello, I was wondering how to limit speed on one axis for an object. I have a script that limits speed for my game object but when the object falls it is also limited.
How can I limit the speed of just one axis? Thanks
What does your existing script do? It shouldn't be very hard to hack that to only affect one axis!
It gets the velocity of the rigidbody and if it is over say, 5, it will then limit it to 5 but it does it for all axis.
Actually, I am not sure of how to do this.
if($$anonymous$$athf.Abs(rigidbody.velocity.y)>maxspeed) rigidbody.velocity.y=maxspeed;
That is the code I use. It limits the speed properly when going in one direction but if I go the negative direction it jerks due to the velocity being directly changed regardless of direction. How could I fix this?
Answer by chemicalvamp · Oct 14, 2011 at 02:23 PM
Checked and double checked this is working for me, and I took out what was unnecessary
using UnityEngine;
public class SlowFall : MonoBehaviour
{
public float maxspeed = 1;
public float speedreduction = 1;
void Update()
{
Vector3 Adjustment = Vector3.zero;
if (transform.rigidbody.velocity.y > maxspeed)
{
Adjustment.y += -speedreduction;
}
if (transform.rigidbody.velocity.y < -maxspeed)
{
Adjustment.y += speedreduction;
}
rigidbody.velocity += Adjustment;
}
}
Yeh that worked great! I don't know what was going wrong with $$anonymous$$e :/
Thanks for all the help!
Answer by chemicalvamp · Oct 14, 2011 at 08:34 AM
Well you could try reducing the amount your velocity is being affected. I think i would try
if(Mathf.Abs(rigidbody.velocity.y) > maxspeed) (rigidbody.velocity.y = rigidbody.velocity.y - 3)
if(Mathf.Abs(rigidbody.velocity.y) < -maxspeed) (rigidbody.velocity.y = rigidbody.velocity.y + 3)
Find the magic number that arrests acceleration and your good to go.
Tried that but when going in the negative direction it speeds off.
it accelerates faster when traveling in a negative direction?
Yep. For some reason, the speed goes really high when travelling in a negative direction even when using the script. It is strange.
Thanks for trying though. All your help so far has been greatly appreciated!