How can I get old position of a moving object?

Hi guys,
My case like this:
Player object is keep moving. When a enemy was seeing him( within 20 units of distance) it will punch attack to the player. But it is not smart enough to update the player’s position. So the enemy only can reach the old position of player when it see the player. Once the enemy reached the old position of player, it should attack player continually if the distance between them is within 20 units.
So far I only can give the updating position of player to enemy.

How can I get an old position of player object to enemy and let it to attack.
I’m not a english speaker so that I draw a pic like below:
[25312-screen+shot+2014-04-16+at+15.03.58.png|25312]

Without seeing your code I can’t really go into specific, plus i’m really tired.

But store a Vector3 variable.

private Vector3 players_Last_Position;
private Transform player;

void Awake()
{
    player = GameObject.FindGameObjectWithTag("Player").transform;
    //Or just make it public and set in the inspector...
}

void TheFunctionWhereYouSeeThePlayer()
{
     if(You have just seen the player)
       {
           players_Last_Position = player.transform.position;
           //Now you have the position of the player when you seen it
           // stored as a variable
       }
}

Just a quick implementation of how you could do it. Hope that helps.

Thank you guys, It’s helpful very much! and you are right I should past my codes.

I’d like to know where to call the “TheFunctionWhereYouSeeThePlayer()”? For now I call it in Update(), but the enemy will catch the updating position of player when play were moving. I am confused.

I define my enemy’s behavior like this: If the distance between it and player is within 20 units, the enemy will stop wandering, stand for one second and chenage color to Red for “angry”. then move “attack” to the old position of player when it saw player. If player was not at the old position and the distance is still within 20 units the enemy would attack player again. This would be loop with 20 units distance.

Sorry for my english.

Thanks,

I past my codes below.(I am a new hand of coding :P)

PS: 1, I use navmesh as the way of wandering. 2, I use iTween.MoveTo to attack.

public class SampleAgentScript : MonoBehaviour {

public Transform Player;
Privat NavMeshAgent agent;
Privat Transform myTransform;
public Vector3 randomTarget;
public float range;
public float distance;
public float colorTime;

void Awake ()
{
myTransform = gameObject.transform;
agent = GetComponent();
range = 4;
colorTime = 1.0f;
Wander();
gameObject.renderer.material.color = Color.white;
}

void Update ()
{
distance = Vector3.Distance(Player.position, gameObject.transform.position);

if (distance < 20)
{
   StartCoroutine(SeeingPlayer());
}


if (distance > 20)
{
   if ((myTransform.position - randomTarget).magnitude < range)
   {
     Wander();
   }
}

}

IEnumerator SeeingPlayer()
{
//set the player position as target Position attacking to;
randomTarget = Player.position;

//change into attcking color;
Color lerpColor = Color.Lerp(gameObject.renderer.material.color, Color.red, Time.deltaTime * 3f);
gameObject.renderer.material.color = lerpColor;

//slow down move speed to zero;
agent.speed -= 0.5f;
if (agent.speed < 0)
   agent.speed = 0;

//wait for 1 second;
yield return new WaitForSeconds(1);

//move to player old position.
iTween.MoveTo(gameObject, iTween.Hash("position", randomTarget,"time", 1f, "looktarget", randomTarget, "easetype", iTween.EaseType.linear));

}

void Wander()
{
//set wander speed;
agent.speed = 3.5f;

//set a random position that enemy move to;
randomTarget = new Vector3((float)Random.Range(-50, 50), 0.0f, (float)Random.Range(-50, 50));
agent.SetDestination(randomTarget);

//change into wander color;
Color lerpColor = Color.Lerp(gameObject.renderer.material.color, Color.white, Time.deltaTime * 40f);
gameObject.renderer.material.color = lerpColor;

}

hi i’ve been dealing with the same problem and i just can’t get the previous position of the player once my enemy has spotted him, it’s always updating so my “playerStoredPosition” is always the player’s current position, i don’y know what to do and i was wonder if you just figured out?? thanks!!

here’s my code:

void Update () {

		if(target.transform.position.x > transform.position.x){

			lookingRight = true;

		}

		if(target.transform.position.x < transform.position.x){

			lookingRight = false;

		}


		if (player.knockback) {

			hurtingPlayer = true;

		} else {
		
			hurtingPlayer = false;
		
		}

		rangeCheck ();


	}

	void rangeCheck(){

			distance = Vector3.Distance (transform.position, target.transform.position);


		if (distance < rangeToFollow) {

			isFollowingThePlayer = true;

			transform.position = Vector3.MoveTowards (transform.position, target.transform.position, moveSpeed * Time.deltaTime);




			if(distance < attackRange) {

				attack = true;

				isFollowingThePlayer = false;

			 targetStoredPosition = target.transform.position;

					transform.position = Vector3.MoveTowards (transform.position, targetStoredPosition, attackingSpeed * Time.deltaTime);




			}



			}



	}