- Home /
Duplicate Question
Rotating the weapon back using Quaternion.Lerp
Hello everyone. I want to rotate my gun to left & right when player moves to left & right. So I made a code like this :
var from : Transform; // a gameObject for start
var to1 : Transform; // a gameObject which is for left limit of rotating ( 0,0,1.5)
var to2 : Transform; // a gameObject which is for right limit of rotating ( 0,0,-1.5)
var orig : Transform; // a gameObject which has the original rotations of gun(0,0,0)
var current : Transform: // a gameObject for the weapons current rotation
var speed = 0.1;
private var rotateLevel : int =0; // a value to stop rotating left
private var rotateLevel2 : int =0; // a value to stop rotating right
function Update(){
if(Input.GetKey("a")){
if(rotateLevel<40){
rotateLevel+=1;
rotateLevel2-=1;
transform.rotation =
Quaternion.Lerp (from.rotation, to1.rotation, Time.deltaTime * speed);
}
}
else if(Input.GetKey("d")){
if(rotateLevel2<40){
rotateLevel-=1;
rotateLevel2+=1;
transform.rotation =
Quaternion.Lerp (from.rotation, to2.rotation, Time.deltaTime * speed);
}
}
else{
?????
}
}
This works well, it moves smoothly & good according to the variables that I adjusted. But when I stop walking, it stays in the position which it was at. So all the aiming and other things get messed up. I need to turn the gun back to it's original rotation smoothly. For that, I made a code like that but didn't work :
else{
transform.rotation =
Quaternion.Lerp (current.rotation , orig.rotation, Time.deltaTime *speed);
}
but as I said it did not work. I need help, any ideas ? Thanks =)
Answer by Berenger · May 12, 2012 at 05:32 AM
Try
transform.rotation = Quaternion.Lerp( transform.rotation , from.rotation, Time.deltaTime *speed);
I'm not sure of what is the diff between from and orig though.
It did not work, I don't know if I wrote the code & if statements in wrong way but when I put Debug.Log in to else{} it works, but when the code I wrote or the code you said just doesn't work. Thanks for the help though, but you got any ideas ? :)