- Home /
Rotation stops when reach the limit.
I have a script of an helicopter and, when Main rotors reach the max speed, they stop to spin. how to solve that?
it's like this:
var rotation = 0;
var speed = 0;
var maxspeed = 20000;
function Update (){
rotation += 0.001 * Time.deltaTime;
speed += rotation * 15;
if(speed > maxspeed){
speed = maxspeed;
}
}
thx any advance.
What code is actually making the rotors spin?
Is this the only code which adjusts these values?
Answer by Bunny83 · Apr 16, 2012 at 09:12 PM
I'm also interested in where you actually rotate the object?
Anyway, i guess your problem is that you use int variables! All say Unityscript (Javascript) is easier, but that's one of the pitfalls ;)
You didn't specify a variable type so the type is determined via type-inference. Since you initialize your variables with integer values (0 and 20000) they become int variables. You need float variable so you can store fractional numbers. You can either give your variables a type manually:
var rotation : float = 0;
var speed : float = 0;
var maxspeed : float = 20000;
or initialize the variables with floats:
var rotation = 0.0;
var speed = 0.0;
var maxspeed = 20000.0;
Answer by hesa2020 · Apr 16, 2012 at 08:38 PM
This is a script you can use, i dont know if it work i dont tested it yet Just name this script : HelicopterRotation.cs Here the script : CLICK TO GET THE SCRIPT
This is a horrible script ;) :
The spin down doesn't work since you rotate only when "On == true".
The RotationSpeed increase is frame dependent.
No way to change the spin-up time / speed
Using int for speed is very strange.
Using the inequality operator to limit an increase or decrease is very bad style. It works only with int values and only when you increase / decrease by 1.
Start should be removed when it's not used. This will increase the performance (well almost nothing, but it's a fact)
Answer by CaptainIcy281 · Apr 16, 2012 at 08:14 PM
It looks like all you need is something like this.
if(speed >= maxspeed) //>= is greater than or equal to
{
speed = 0;
}
And handle the rotation as you normally would. All you need is to tell it to "stop" when it reaches the max speed.
Or do you need something more than this? I didn't exactly understand your question perfectly.