x


[Closed] 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 =)

more ▼

asked May 11 '12 at 07:46 PM

Inan Evin gravatar image

Inan Evin
166 37 58 64

(comments are locked)
10|3000 characters needed characters left

The question has been closed May 12 '12 at 08:29 AM by Inan Evin for the following reason:

Duplicate Question


1 answer: sort voted first

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.

more ▼

answered May 12 '12 at 05:32 AM

Berenger gravatar image

Berenger
11k 12 19 53

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 ? :)

May 12 '12 at 08:03 AM Inan Evin
(comments are locked)
10|3000 characters needed characters left

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x720
x437
x261
x217

asked: May 11 '12 at 07:46 PM

Seen: 697 times

Last Updated: May 12 '12 at 08:03 AM