Vector3.Lerp for GameObjects

Hey there guys, I’m having an issue with a Vector3.Lerp function I am trying to call, I am trying to achieve an aiming mechanic for a fpc gun, but hate the instant transfer from being ‘hip fired’ to aiming, I’ve tried a couple of different options now, but none seem to be looking up, here is what i have at the present.

    public void aim()
    {
        gameObject.transform.position = Vector3.Lerp(handGO.transform.position, aimGO.transform.position, Time.deltaTime * smooth);
        gameObject.transform.rotation = aimGO.transform.rotation;
    }

    public void unaim()
    {
        gameObject.transform.position = Vector3.Lerp(aimGO.transform.position, handGO.transform.position, Time.deltaTime * smooth);
        gameObject.transform.rotation = handGO.transform.rotation;
    }

The third parameter for Vector3.Lerp is the point in between the from and to parameters. If you set it as 0 it will return the “from” vector (the first one) and if you set it as 1 it will return the “to” (the second vector). So what you want it to be is to start from 0 and progressively increase to 1. But when you multiply Time.deltaTime * smooth you will get an almost constant number, let’s say Time.deltaTime equals 0.02 and smooth equals 5, that will give you 0.1 and will stay like that or vary a little. What you need to do is to create a float variable that starts at 0 and add the Time.deltaTime * smooth so it will increase towards 1.

Like carle13 said, if you use Time.deltaTime * smooth as t (lerp(a,b,t)), you will get closer and closer to b, but you’ll never really get there. However, you may achieve a more natural aim/deaim if you do this compared to a linear movement. Your current code only has two very small errors. Just change these two lines:

gameObject.transform.position = Vector3.Lerp(handGO.transform.position, aimGO.transform.position, Time.deltaTime * smooth);
gameObject.transform.position = Vector3.Lerp(aimGO.transform.position, handGO.transform.position, Time.deltaTime * smooth);

Instead of lerping from the beginning and end point by Time.deltaTime * smooth, lerp from the current position to either the beginning or end point, like this:

gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, aimGO.transform.position, Time.deltaTime * smooth);
gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, handGO.transform.position, Time.deltaTime * smooth);

If you would like to linearly interpolate between the two positions, send me a message and I’d be glad to provide the code.