How do I make a gun aim down sights?

I have a problem where a script that is just supposed lerp the gun object between a hip and zoomed firing position isn’t working at all.

using UnityEngine;
using System.Collections;

public class GunZoom : MonoBehaviour {

	public Vector3 hipPos;
	public Vector3 zoomPos;

	void Update () {
		if(Input.GetButton("Zoom")) {
			transform.localPosition = Vector3.Lerp(transform.localPosition, zoomPos, Time.deltaTime);
		}
		else {
			transform.localPosition = Vector3.Lerp(transform.localPosition, hipPos, Time.deltaTime);
		}
	}
}

The “Zoom” button is just the right mouse button. When I click it, nothing happens. The gun is a child of the camera, which is a child of the player object. Any ideas?

Well, Time.deltaTime in that lerp may be a bit slow. Even when you hold the button down, does it move?

Don’t know how you set up the animation so you have to figure out that part on your own.

But what I do know is that you used Vector3.Lerp wrong.

Vector3.Lerp works like percentage of completion. You can’t put Time.deltaTime there because it wouldn’t make any sense.

When it’s 0, the object is at its original position.

When it’s 1, the object is at the destination.

When it’s 0.5, the object is half way between the original position and destination.

Instead of using Vector3.Lerp use Vector.MoveTowards

It also gives the gun a more snap to response than still slowly moving when ADS.