How do I Lerp with one call to the function?

Hello I’ve recently started scripting and I’ve been making an fps game and I’ve started to run to a lot of issues which is helping me since the trial and error processes makes me learn more and more with the community.

My question is how would I go about smoothly transitioning and objects local transform here is a script below but the object will only move a little bit since the Lerp is called only once instead of multiple times but I am not sure how to call it multiple times until the lerp is done?

 if (Input.GetMouseButtonDown(1))
        {
            Transform gunTrans;
            Vector3 NormalPos;
            bool IsAiming = false;
            if (IsAiming)
            {
                IsAiming = false;
                gunTrans.localPosition = Vector3.Lerp(gunTrans.localPosition, NormalPos, AimSpeed);
            }
            else if (!IsAiming)
            {
                IsAiming = true; // If not obvious AimVector3 is the position I want to move the gun to the aim speed is the duration which is a float that is .25 seconds long.
                gunTrans.localPosition = Vector3.Lerp(gunTrans.localPosition, AimVector3, AimSpeed);
            }
        }

Anyhow you can see what I mean its a basic branch type script with two if statements it will switch them between once I right click if I was aiming I aim out if I Wasn’t aiming it will aim? Any help is appreciated thanks for any help!

Usually you set up a condition and then in the Update() loop you adjust it a little bit each frame based on that condition.

Looking at your code above, it seems like if you simply toggled the IsAiming boolean (make it a private class variable, NOT located inside your Update() function) when the mouse button is pressed, then ALWAYS do the check to see if you are aiming or not, it would do what you want.

Something like, (just coding in English, not actual code):

void Update()
{
  if (mousedown)
  {
    isAiming = !isAiming; // toggles it!
  }

  if (isAiming)
  {
    // move gun to one point
  }
  else
    // move gun to the other point
  }
}
1 Like

Thanks I have done that in code and worked great thanks for all the help recently.

1 Like