Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Don't miss out on the Unite LA Keynote for the latest information from Unity! Set a reminder!
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    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 :)