Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question How do I make an Enemy attack after a trigger?

Discussion in '2D' started by demeterperse, May 10, 2024.

  1. demeterperse

    demeterperse

    Joined:
    May 10, 2024
    Posts:
    1
    So I want my player to collide with an object and after that I want my enemy to start attacking and chasing the player. I have 2 scripts on my Enemy and the object I want the player to collide with.

    Enemy Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyFollow : MonoBehaviour
    6. {
    7.     public GameObject player;
    8.     public float speed;
    9.  
    10.     private float distance;
    11.  
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.        
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         distance = Vector2.Distance(transform.position, player.transform.position);
    23.         Vector2 direction = player.transform.position - transform.position;
    24.  
    25.         transform.position = Vector3.MoveTowards(this.transform.position, player.transform.position, speed * Time.deltaTime);
    26.  
    27.        
    28.     }
    29.      
    30. }
    Collision Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Colliding : MonoBehaviour
    6. {
    7.     public
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.        
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.  
    18.     }
    19.        private void OnCollisonEnter2D(Collision2D collision)
    20.       {
    21.         if(collision.gameObject.CompareTag("Player"));
    22.        
    23.        
    24.       }
    25.    
    26. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,335
    I like this one-step-at-a-time approach. It works well with complex technical issues:

    Imphenzia: How Did I Learn To Make Games:



    If you run into trouble...

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?