Storing a vector position and updating it every fraction of a second.

So I have this turret.

As long as the player is in line of sight of this turret (Say…bool PlayerInSight = true which is checked in Update)

the turret takes the player’s transform position at a frame(Vector3 spottedPos = player.transform.position)

it should store that Vector3 for say. 0.3 seconds without changing it frame by frame.

then 0.3 seconds pass, if PlayerInSight is still true, THEN the script updates spottedPos

and this sequence repeats itself every 0.3 seconds for as long as PlayerInSight = true. If PlayerInSight = false,

I’ve been trying to pull this off with InvokeRepeating and CancelInvoke but not only was spottedPos still updating frame-by-frame but its interval varies widely instead of being a fixed 0.3 second duration.

Ah, sounds like you’re in need of a Coroutine!

	IEnumerator UpdatePosition()
	{
		//whatever you want to do
		yield return new WaitForSeconds (0.3f);
		StartCoroutine (UpdatePosition ());
	}

Coroutines are very similar to functions. They can be made to wait any length of time, you can change the WaitForSeconds value to adjust this in the example. To call a Coroutine use StartCoroutine(); In the example the Coroutine is waiting 0.3 seconds before calling itself (you will of course have to call it in the first place before it does anything).

Check out the manual page on Coroutines: Unity - Manual: Coroutines