# I want to lerp a rotation?

``````using UnityEngine;
using System.Collections;

public class Theshoot : MonoBehaviour {

public Rigidbody bulletPrefab;
public Transform barrelEnd;
public GameObject gun;
public float Recoil = 10f;

void Update ()
{
if(Input.GetButtonDown("Fire1"))
{
Rigidbody rocketInstance;
rocketInstance = Instantiate(bulletPrefab, barrelEnd.position,
bulletPrefab.transform.rotation) as Rigidbody;
gun.transform.Rotate(0, 0, 10);
gun.transform.Rotate(0, 0, -10);
}
}
}
``````

You can interpolate rotation using Quaternion.Slerp.

I’m better at writing code examples instead of explaining so try something like this:

``````private Vector3 angle = Vector3.zero;
private Vector3 recoil = new Vector3(?,?,?); //You'll have to test for these values

void Update(){
//Slerp to the angle vector, which is default 0,0,0
//Set it to the guns default local rotation if it isn't 0,0,0
gun.transform.localEulerAngles = Vector3.Slerp(gun.transform.localEulerAngles, angle, Time.deltaTime);

if(Input.GetButtonDown("Fire1")){
//Instantiate rocket, etc.
gun.transform.localEulerAngles += recoil;
}

}
``````

This should theoretically keep the gun rotated to default, but when you shoot it will instantly add some rotation to it, and it will then have to lerp back to the original position. It won’t look very good but it should work.

Also, with what Dave has said - quaternions are a lot better for spherical interpolation than vectors, they can slerp in a straight line from one rotation to another while vectors will result in a slight curve. The only thing with quaternions is that they’re a lot more complex than vectors, so you can’t simply add a single value to rotate it for recoil.