Why when I move a child object (a weapon) using Vector3.Lerp does it not return to its original position when walking around with the below code? All I’m trying to do is move my weapon up close to the players face, I also have a script that zooms the camera in so it looks like you are zooming into the target.
Any help would be appreciated, thanks.
#pragma strict
private var globalVars : GameObject; // var for the GameObject with the GlobalVars on
function Awake(){
globalVars = GameObject.Find("Global Vars"); // Find the gameObject with the GlobalVars attached to it
}
function Start () {
}
function Update () {
if (Input.GetButtonDown("Zoom In")){
StartCoroutine(MoveToPosition(globalVars.GetComponent(GlobalVars).currentWeapon.transform.position + Vector3(-0.25, 0.05, 0), 1));
}
if (Input.GetButtonDown("Zoom Out")){
StartCoroutine(MoveToPosition(globalVars.GetComponent(GlobalVars).currentWeapon.transform.position + Vector3(0.25, -0.05, 0), 1));
}
}
function MoveToPosition(newPosition : Vector3, time : float)
{
var elapsedTime : float = 0;
var startingPos = globalVars.GetComponent(GlobalVars).currentWeapon.transform.position;
while (elapsedTime < time)
{
globalVars.GetComponent(GlobalVars).currentWeapon.transform.position = Vector3.Lerp(startingPos, newPosition, (elapsedTime / time));
elapsedTime += Time.deltaTime;
yield;
}
}