Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Enemy follow the character and push it back

Discussion in 'Physics' started by Tori_Aldana, Aug 5, 2018.

  1. Tori_Aldana

    Tori_Aldana

    Joined:
    Jul 2, 2018
    Posts:
    2
    Hello!

    I'm trying to do a very simple enemy behaviour in a 2D topDown. It will movetowards the character and when enter on collision make damage. This is the enemy code:
    Code (CSharp):
    1. public class EnemyDamage : MonoBehaviour
    2. {
    3.  
    4.     private DamageSystem damageSystem;
    5.     private GameObject player;
    6.     private Transform target;
    7.     public float speed = 3;
    8.  
    9.     private void Awake()
    10.     {
    11.         player = GameObject.FindGameObjectWithTag("Player");
    12.         damageSystem = player.GetComponent<DamageSystem>();
    13.         target = player.GetComponent<Transform>();
    14.     }
    15.  
    16.     private void Update()
    17.     {
    18.         transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
    19.     }
    20.  
    21.     private void OnCollisionEnter2D(Collision2D collision)
    22.     {
    23.         if (collision.gameObject == player)
    24.         {
    25.             damageSystem.AddHealth(-15);
    26.         }
    27.     }
    28. }
    My problem is that when enemy collides with the character it moves! I tried to put isKinematic to the player, but doing that both sprites are in the same position (I would need enemy respects the collider).

    As I understand, as enemy tries to reach the position and it's colliding with the player is for this reason that enemy push the character.

    Any idea to resolve this?
     
  2. Tori_Aldana

    Tori_Aldana

    Joined:
    Jul 2, 2018
    Posts:
    2
    Hello!

    I have resolved it by changing the speed on the collision methods. I'm posting the code in case anyone find useful!
    Code (CSharp):
    1. public class EnemyDamage : MonoBehaviour
    2. {
    3.  
    4.     private DamageSystem damageSystem;
    5.     private GameObject player;
    6.     private Transform target;
    7.     public float speed = 2;
    8.     private float speedCalculated;
    9.  
    10.     private void Awake()
    11.     {
    12.         player = GameObject.FindGameObjectWithTag("Player");
    13.         damageSystem = player.GetComponent<DamageSystem>();
    14.         target = player.GetComponent<Transform>();
    15.         speedCalculated = speed;
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.         transform.position = Vector2.MoveTowards(transform.position, target.position, speedCalculated * Time.deltaTime);
    21.     }
    22.  
    23.     private void OnCollisionEnter2D(Collision2D collision)
    24.     {
    25.         if (collision.gameObject == player)
    26.         {
    27.             damageSystem.AddHealth(-15);
    28.             speedCalculated = 0;
    29.         }
    30.     }
    31.  
    32.     private void OnCollisionExit2D(Collision2D collision)
    33.     {
    34.         speedCalculated = speed;
    35.     }
    Now I'm looking for the enemy waits a moment before continuing chasing the player and add a time between attacks, but this is another topic!

    Thanks :)