- Home /
Question by
DavidII4VG · Sep 28, 2013 at 05:35 AM ·
androiddebug
Help setting maximum velocity
I have been trying but for some reason nothing works when i try and set a maxSpeed for my spaceship. could some help me please?
var forwardThumbPad : GUITexture;
private var thisTransform : Transform;
var forwardSpeed : float = 200;
var vectorForceX : Vector3;
var accelRatio : float = .1;
var maxShipSpeed : float;
@HideInInspector
function Start () {
thisTransform = GetComponent(Transform);
}
function Update () {
if(Input.touches.Length <= 0)
{
}
else
{
for(var i = 0; i < Input.touchCount; i++)
{
if(this.forwardThumbPad.HitTest(Input.GetTouch(i).position))
{
if(Input.GetTouch(i).phase == TouchPhase.Stationary)
{
var movement = thisTransform.TransformDirection(Vector3.zero);
movement.z += forwardSpeed;
var force = vectorForceX * movement.z;
rigidbody.AddForce(force);
}
if(Input.GetTouch(i).phase == TouchPhase.Ended)
{
}
}
}
}
}
Comment
Answer by robertbu · Sep 28, 2013 at 05:39 AM
There are a couple of things you can do. First you can just stop adding force if the velocity is at or over your threshold. So line 38 becomes:
if (rigidbody.velocity.magnitude < maxShipSpeed)
rigidbody.AddForce(force);
Another way is to limit the velocity directly. At like 40, put:
if (rigidbody.velocity.magnitude > maxShipSpeed)
rigidbody.velocity = rigidbody.velocity.normalized * maxShipSpeed;
Thank you Robert, now i understand how to set a max speed with a rigidbody. =)