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

H E L P me! with collisions!

Discussion in '2D' started by zouspants, Dec 30, 2019.

  1. zouspants

    zouspants

    Joined:
    Oct 13, 2019
    Posts:
    3
    Hey as i wrote at the title i need help with collisions in making a game like "AGE OF WAR" and im getting the problem that when my character collides with an enemy he stands still and make his attack animation but whenever i remove (destory) the enemy my character doesnt move forward. I started today and i've looked everywhere for a solution like 3 hours now and i cant find one so i am asking you guys if u could help me! would be appreciated to the max!
     
  2. S3M1CZ

    S3M1CZ

    Joined:
    Feb 11, 2017
    Posts:
    14
    Try creating a bool (for example) hasCollidedWithEnemy.
    In Update create if statement that decides if player should move or fight (based on the previous bool)
    The bool should be changed by OnTriggerEnter2D and OnTriggerExit2D (assuming you're working in 2D)
    Also make sure that the player and enemies have proper Rigidbodies.

    I've quickly tried to replicate the idea and it worked for me, try something like this:
    Code (CSharp):
    1. bool hasCollidedWithEnemy = false;
    2.  
    3.     private void Update()
    4.     {
    5.         if (hasCollidedWithEnemy == false)
    6.         {
    7.             Debug.Log("moving");
    8.         }
    9.         else
    10.         {
    11.             Debug.Log("fighting");
    12.         }
    13.     }
    14.  
    15.     private void OnTriggerEnter2D(Collider2D collision)
    16.     {
    17.         if (collision.tag == "Enemy")
    18.         {
    19.             hasCollidedWithEnemy = true;
    20.         }
    21.     }
    22.  
    23.     private void OnTriggerExit2D(Collider2D collision)
    24.     {
    25.         if (collision.tag == "Enemy")
    26.         {
    27.             hasCollidedWithEnemy = false;
    28.         }
    29.     }