- Home /
If (increasing/decreasing)? check if something is increasing or decreasing
i am using the mouselook from the fps tut(i think)
and im trying to achieve a slight tilt with my gun.
when i look left, i want my gun to slightly tilt left when i look right... and so on.
i understand how to tilt my gun but i dnt know how to get it to tilt with the mouse look script
i understand that it is the rotationX value but how can i say in code,
if(rotationX is increasing) this will be it looking right.
if(rotationX is decreasing) this will be it looking left.
Answer by jahroy · May 12, 2011 at 04:37 PM
It looks like rotationX simply adds to itself the current value of Input.GetAxis("Mouse X") every time Update is called.
Input.GetAxis("Mouse X") will be negative when the mouse is moving left. It will be positive when the mouse is moving right.
Therefore, you could do something like this:
function Update () { [...]
if ( Input.GetAxis("Mouse X") > 0 ) {
tiltGunRight();
}
else {
tiltGunLeft();
}
}
Another way you could do this would be to use a class variable that keeps track of what the last value of rotationX was:
private var lastRotationValue;
function Update () {
rotationX += Input.GetAxis("Mouse X") * sensitivity;
if ( rotationX > lastRotationValue ) {
tiltGunRight();
}
else {
tiltGunLeft();
}
lastRotationValue = rotationX;
}
Fortuntately you don't have to do that here because the Input.GetAxis function makes it even easier!