# Storing player's previous position?

Hello,

I have an enemy snake that performs an attack animation each time player triggers the collider. However the damage player takes is instant, and animation has a 1.5 second delay. I thought about storing player’s position and then comparing it after 1.5 second delay, and if player’s (current x/y minus x/y) axis will be |2| or less then player takes damage, because he’s in range.

To store player’s location I’m using Vector 3 and my overall code looks like.

Vector3 oldPosition;

``````    void OnTriggerEnter2D(Collider2D col){
if(col.CompareTag("Player")){
player.Damage (1);
snakeAttack = true;

oldPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);

}

}
``````

In my Update method I have this line

``````if (snakeAttack) {
snakeTimer += Time.deltaTime;
}
if (snakeTimer >= 0.8) {
snakeAttack = false;
snakeTimer = 0;

}
``````

How do check player’s coordinates and COMPARE it with what I have in ‘oldPosition’?
Or is there a different way of doing this? Help please!

Regards,
Aleksei D.

I wish it was something easy like

``````if( Math.abs(oldPosition.transform.x - transform.position.x) <= 2 && Math.abs(oldPosition.transform.y - transform.position.y) <= 2)
player.damage(1);
``````

I wonder if this will work

``````if(new Vector3(transform.position.x,transform.position.y,transform.position.z)<=oldPosition){
``````

I’m not sure why you’d save the player’s old position of where they were located when they triggered the snake’s attack? If the player comes within range and triggers the snake attack it seems like the way to handle it would be to do the snake attack sequence and for the few frames of the snake strike or whatever… at that point in time check to see if the player is within range. Alternately just process the player character’s auto result of being attacked while running the snake attack sequence.

My area that triggers the OnTriggerEnter2D is basicly the outline of the snake enemy, thus I need the old pos and then I need to compare if player is within the range. (I check old coordinates with current coordinates, if they’re within 2 px, then player is within the attack range)
However, I see that there’s a different was of doing this? Can you be more specific please?
I will change the trigger area to area in front of the snake (attack range), and that should only trigger the attack animation. Then what? How do I check if the player is within that range JUST for the Player.damage(1); method?

Alright so I did as you suggested and used Triggers instead. So far so good. My snake does it’s attack animation and player gets hurt ONLY if he’s within it’s attack range.
My SnakeAI script

``````using UnityEngine;
using System.Collections;

public class EnemySnake : MonoBehaviour {

private Player player;
private Animator anim;
public bool snakeAttack = false;
public float snakeTimer = 0;
float attackTime = 0.85F;
bool getHit = false;
Vector3 oldPosition;
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag ("Player").GetComponent<Player> ();
anim = gameObject.GetComponent<Animator> ();

}

//if we collide with the player tag.
void OnTriggerEnter2D(Collider2D col){
if(col.CompareTag("Player")){
//player.Damage (1);
snakeAttack = true;

// oldPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);

}

}

void OnTriggerStay2D(Collider2D col){
if(col.CompareTag("Player")){
if(getHit){
player.Damage (1);
getHit = false;
//StartCoroutine(player.KnockBack(0.0001f, 190, player.transform.position));
}

}

}

void OnTriggerExit2D(Collider2D col){
if (col.CompareTag ("Player")) {

if(snakeTimer < attackTime){
snakeTimer = 0;
getHit = false;
}

}

}

// Update is called once per frame
void Update () {
anim.SetBool ("snakeAttack", snakeAttack);

if (snakeAttack) {
snakeTimer += Time.deltaTime;
}
if (snakeTimer >= attackTime) {
snakeAttack = false;
snakeTimer = 0;
getHit = true;

}

}
}
``````
1 Like