- Home /
Move speed decrease stoping object problem.
Hello so my problem is that i want to decresse object speed after some circumstances and my script is going crazy and never run is with min speed ... here is code from update function, void Update () {
Vector3 move = Vector3.zero;
move += Vector3.forward * Input.GetAxis (vAxis) * airRotationSpeed * Time.deltaTime;
transform.Rotate (move);
if (upDownIncLast > transform.position.y) {
airSpeed = airSpeed + 0.1f;
}
if (upDownIncLast < transform.position.y) {
airSpeed = airSpeed - 0.1f;
}
if (airSpeed < 0){
airSpeed = 0.1f;
}
upDownIncLast = transform.position.y;
transform.Translate (Vector2.right * airSpeed *Time.deltaTime);
}
Maybe someone can help solve me this out.
Answer by GameVortex · Jan 08, 2014 at 10:11 AM
You need to add Time.deltaTime to your speed calculation or the airSpeed value will increase or decrease extremely fast based on your framerate.
Do this:
if (upDownIncLast > transform.position.y) {
airSpeed = (airSpeed + 0.1f) * Time.detlaTime;
}
else if (upDownIncLast < transform.position.y) {
airSpeed = (airSpeed - 0.1f) * Time.deltaTime;
}
Hopefully your values will not be all over the place then.
it's still not working correcly maybe my check is wrong could you tell me how to detect if object is moving up or down?
In your case your object is moving up if transform.right.y is positive and down if transform.right.y is negative.
Do a check if the y value is above or below 0. Above 0 is positive values and below zero are negative values.