Vector3.lerp doesn't work in local position

Hi Everyone,

I have a problem with Lerp, I want smoothly move between two points in local position and i can’t understand why my code not working. Please help me.

Here is my code:

    public Vector3 HipPose;
	public Vector3 AimPose;
	public float AimingZoom = 50f;
	public float AimSpeed = 5.0f;
	private GameObject MainCam;
	private GameObject GunCam;

	void Start () 
	{
		transform.localPosition = HipPose;
		MainCam = GameObject.FindGameObjectWithTag("MainCamera");
		GunCam = GameObject.FindGameObjectWithTag("gunCamera");
	}
	
	// Update is called once per frame
	void Update () 
	{
		if(Input.GetKey(KeyCode.Q))
		{
			MainCam.camera.fieldOfView = AimingZoom;
			GunCam.camera.fieldOfView = AimingZoom;

			transform.localPosition = AimPose;
			transform.localPosition = Vector3.Lerp(transform.localPosition, AimPose, AimSpeed * Time.deltaTime);
		}
		if(!Input.GetKey(KeyCode.Q))
		{
			MainCam.camera.fieldOfView = 60;
			GunCam.camera.fieldOfView = 60;

			transform.localPosition = HipPose;
			transform.localPosition = Vector3.Lerp(transform.localPosition, HipPose, AimSpeed * Time.deltaTime);
		}
	}

1 Answer

1

You can get your current code working by deleting lines 23 and 31. You are setting the local position to the end position before you even do the Lerp(). Also line 26 can be replaced by ‘else’.

robertbu, thank you so much, now all works properly. Have a nice day. :)

–