Add a Delay to Vector3.Lerp

I have a top down shooter game where if you click/touch the screen the camera zooms in and follows your mouse/touch. It then goes back to it’s original position upon release. I have the Vector3.Lerp inside of LateUpdate but would like to add a delay so that the camera isn’t zooming in and out on semi-automatic fire style shots (just tapping a random enemy here and there). Basically if you hold down for more than half a second then it will zoom.

Is there a way to add a delay to either Vector3.Lerp or possibly call the action outside of LateUpdate. Or could I somehow say “if(Input.GetMouseButton(0) && holdDownTime is > .5f){ do action}”?

Here is What I got:

#pragma strict

var Damping = 5.0;
var Player : Transform;
var Height1 : float;
var Height2 : float;
var Offset : float;
var ViewDistance : float = 5.0;
private var Center : Vector3;

function LateUpdate (){

var mousePos = Input.mousePosition;
mousePos.z = ViewDistance;
var CursorPosition : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
var PlayerPosition = Player.position;

if(Input.GetMouseButton(0))

{
Center = Vector3((PlayerPosition.x + CursorPosition.x) / 2, PlayerPosition.y, (PlayerPosition.z + CursorPosition.z) / 2);
transform.position = Vector3.Lerp(transform.position, Center + Vector3(0,Height1,Offset), Time.deltaTime * Damping);
}

else//(Input.GetMouseButtonUp)
{
transform.position = Vector3.Lerp(transform.position, Vector3(0,Height2,Offset), Time.deltaTime * Damping);
}

}
  1. Use code tags. Check the sticky posts in this forum.
  2. You’re abusing Lerp. It will sometimes work, sometimes not, depending on the frame rate at runtime. Please use SmoothDamp or MoveTowards instead.
  3. To put in a delay, you’ll need to make the code a bit more complex… you’ll need a property to keep track of how long the camera has been away from its target, and update it as follows:
  • if we’re at the target, reset our timeAway property to 0

  • otherwise, add Time.deltaTime to our timeAway property

  • if timeAway > some threshold, then MoveTowards the target

Good luck,

  • Joe