Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Storing player's previous position?

Discussion in '2D' started by FirefIy, Apr 27, 2015.

  1. FirefIy

    FirefIy

    Joined:
    Apr 15, 2015
    Posts:
    23
    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;

    Code (csharp):
    1.  
    2.     void OnTriggerEnter2D(Collider2D col){
    3.         if(col.CompareTag("Player")){
    4.             player.Damage (1);
    5.             snakeAttack = true;
    6.  
    7.             oldPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
    8.  
    9.  
    10.         }
    11.        
    12.     }

    In my Update method I have this line

    Code (csharp):
    1.  
    2. if (snakeAttack) {
    3.             snakeTimer += Time.deltaTime;
    4.         }
    5.         if (snakeTimer >= 0.8) {
    6.             snakeAttack = false;
    7.             snakeTimer = 0;
    8.  
    9.        
    10.         }
    11.    
    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.
     
  2. FirefIy

    FirefIy

    Joined:
    Apr 15, 2015
    Posts:
    23
    I wish it was something easy like

    Code (csharp):
    1. if( Math.abs(oldPosition.transform.x - transform.position.x) <= 2 && Math.abs(oldPosition.transform.y - transform.position.y) <= 2)
    2. player.damage(1);
    3.  
     
  3. FirefIy

    FirefIy

    Joined:
    Apr 15, 2015
    Posts:
    23
    I wonder if this will work
    Code (csharp):
    1.  
    2. if(new Vector3(transform.position.x,transform.position.y,transform.position.z)<=oldPosition){
    3.  
     
  4. GarBenjamin

    GarBenjamin

    Joined:
    Dec 26, 2013
    Posts:
    7,441
    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.
     
  5. FirefIy

    FirefIy

    Joined:
    Apr 15, 2015
    Posts:
    23
    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?
     
  6. FirefIy

    FirefIy

    Joined:
    Apr 15, 2015
    Posts:
    23
    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
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class EnemySnake : MonoBehaviour {
    6.    
    7.     private Player player;
    8.     private Animator anim;
    9.     public bool snakeAttack = false;
    10.     public float snakeTimer = 0;
    11.     float attackTime = 0.85F;
    12.     bool getHit = false;
    13.     Vector3 oldPosition;
    14.     // Use this for initialization
    15.     void Start () {
    16.         player = GameObject.FindGameObjectWithTag ("Player").GetComponent<Player> ();
    17.         anim = gameObject.GetComponent<Animator> ();
    18.        
    19.     }
    20.    
    21.     //if we collide with the player tag.
    22.     void OnTriggerEnter2D(Collider2D col){
    23.         if(col.CompareTag("Player")){
    24.             //player.Damage (1);
    25.             snakeAttack = true;
    26.  
    27.             // oldPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
    28.  
    29.  
    30.         }
    31.        
    32.     }
    33.  
    34.     void OnTriggerStay2D(Collider2D col){
    35.         if(col.CompareTag("Player")){
    36.             if(getHit){
    37.             player.Damage (1);
    38.                 getHit = false;
    39.                 //StartCoroutine(player.KnockBack(0.0001f, 190, player.transform.position));
    40.             }
    41.  
    42.            
    43.            
    44.         }
    45.  
    46.  
    47.     }
    48.  
    49.     void OnTriggerExit2D(Collider2D col){
    50.         if (col.CompareTag ("Player")) {
    51.  
    52.             if(snakeTimer < attackTime){
    53.                 snakeTimer = 0;
    54.                 getHit = false;
    55.             }
    56.        
    57.         }
    58.  
    59.     }
    60.    
    61.     // Update is called once per frame
    62.     void Update () {
    63.         anim.SetBool ("snakeAttack", snakeAttack);
    64.  
    65.         if (snakeAttack) {
    66.             snakeTimer += Time.deltaTime;
    67.         }
    68.         if (snakeTimer >= attackTime) {
    69.             snakeAttack = false;
    70.             snakeTimer = 0;
    71.             getHit = true;
    72.  
    73.         }
    74.    
    75.  
    76.  
    77.  
    78.        
    79.     }
    80. }
     
    theANMATOR2b likes this.