Vector3.Lerp move weapon up to players face

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;
    }
}

If the wapon is a child of the player, maybe you want to move the localPosition instead?

If anyone else gets stuck with something like the problem I had here is the answer …

    #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")){
 var zoomInOffset = Vector3(-0.25, 0.05, 0);
 var zoomInWorldPosition = transform.TransformDirection( zoomInOffset );
 StartCoroutine(MoveToPosition(globalVars.GetComponent(GlobalVars).currentWeapon.transform.position + zoomInWorldPosition, 0.5));
 }

if (Input.GetButtonDown("Zoom Out")){
 var zoomOutOffset = Vector3(0.25, -0.05, 0);
 var zoomOutWorldPosition = transform.TransformDirection( zoomOutOffset );
 StartCoroutine(MoveToPosition(globalVars.GetComponent(GlobalVars).currentWeapon.transform.position + zoomOutWorldPosition, 0.5));
 }
}

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;
    }
}