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. Dismiss Notice

Enemy following when in certain radius

Discussion in 'Scripting' started by MCamac55, Jan 8, 2015.

  1. MCamac55

    MCamac55

    Joined:
    Jan 8, 2015
    Posts:
    22
    I have the enemy following a patrol route using 2 waypoints and a Raycast. Would there be a way to get the enemy to stop following the route and start following the player when it is inside the radius? But if the player gets outside the radius, the enemy stops following and goes back to the route?

    This is my patrol script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyAI : MonoBehaviour
    5. {
    6.     public Transform[] Waypoints;
    7.     public float Speed;
    8.     public int curWayPoint;
    9.     public bool doPatrol = true;
    10.     public Vector3 Target;
    11.     public Vector3 MoveDirection;
    12.     public Vector3 Velocity;
    13.  
    14.     private NavMeshAgent agent;
    15.  
    16.  
    17.     void OnTriggerEnter(){
    18.         agent.SetDestination (Target);
    19.         }
    20.  
    21.     void Update()
    22.     {
    23.         agent = GetComponent<NavMeshAgent>();
    24.         agent.SetDestination (Target);
    25.         {
    26.             agent.SetDestination (Target);
    27.         }
    28.  
    29.         if (curWayPoint < Waypoints.Length)
    30.         {
    31.             Target = Waypoints[curWayPoint].position;
    32.             MoveDirection = Target - transform.position;
    33.             Velocity = rigidbody.velocity;
    34.  
    35.             if(MoveDirection.magnitude < 1)
    36.             {
    37.                 curWayPoint++;
    38.             }
    39.             else
    40.             {
    41.                 Velocity = MoveDirection.normalized * Speed;
    42.             }
    43.         }
    44.         else
    45.         {
    46.             if(doPatrol)
    47.             {
    48.                 curWayPoint=0;
    49.             }
    50.             else
    51.             {
    52.                 Velocity = Vector3.zero;
    53.             }
    54.         }
    55.         rigidbody.velocity = Velocity;
    56.         transform.LookAt (Target);
    57.     }
    58.  
    59. }
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    I would place a trigger, sphere in the player transform. Expand the radius of the sphere to the appropriate radius you want the enemy to pursue in.
     
  3. MCamac55

    MCamac55

    Joined:
    Jan 8, 2015
    Posts:
    22
    How would I go about coding this?
     
  4. SevenHams

    SevenHams

    Joined:
    Dec 27, 2012
    Posts:
    67
    One thing you could do is have the enemy periodically check its distance to the player. If the distance is within a certain amount it ignores its patrol. I think that would be a simpler solution as the only thing you seem to care about is if the enemy is within a certain distance. No need to worry about sphere colliders or triggers in this situation I think.
     
  5. MCamac55

    MCamac55

    Joined:
    Jan 8, 2015
    Posts:
    22
    Would using the Raycast for this be easier?
     
  6. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    You just put a trigger on your sphere and then, in a script, you implement OnTriggerEnter and OnTriggerExit.
     
  7. MCamac55

    MCamac55

    Joined:
    Jan 8, 2015
    Posts:
    22
    Thank you very much!
     
  8. SevenHams

    SevenHams

    Joined:
    Dec 27, 2012
    Posts:
    67
    Not really. The simplest solution to this is to have the enemy find the player object, check the distance between the two, and then make a decision based on the distance. The trigger is an alternate solution but the issue there is that it is likely more expensive on processor resources. That however is an optimization thing and if you're just one person making a game by yourself it isn't likely that you'll tax the processor that badly. Both solutions will function properly.
     
  9. MCamac55

    MCamac55

    Joined:
    Jan 8, 2015
    Posts:
    22
    So, I've tried to get it to work a ton of times attempting to use the sphere trigger and I cannot get it to work. I seem to be having a problem with it not colliding for one, and for two I have no clue how I would get it to change paths.

    How I know that it is not colliding is that I set a Debug.Log to send me a message when I collide with it and it is not sending anything.
     
  10. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Hopefully you added a Sphere Collider on your gameObject and you checked "Is Trigger" ?

    Then you should add a script with some Debug.Log (OnTriggerEnter + OnTriggerExit) just to see if everything is set up.
     
  11. MCamac55

    MCamac55

    Joined:
    Jan 8, 2015
    Posts:
    22
    Yeah, I did all the sphere collider stuff and the is trigger stuff. My code is like this:
    Code (CSharp):
    1.     void OnTriggerEnter (Collider other)
    2.     {
    3.                 Debug.Log ("Triggered");
    4.     }
     
  12. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    I'm not sure about that but just try: I think the object which enters inside the trigger must have also a Trigger or a Rigibody.

    Edit: according to the document, each object should have a Collider and/or a Rigibody.

    Also, if it does work, you should check the PhysicalManager ; Edit > Project Settings > Physics (Layer Collision Matrix)
     
  13. MCamac55

    MCamac55

    Joined:
    Jan 8, 2015
    Posts:
    22
    Yeah, I have both the Enemy and the Player having a trigger and a Rigid Body.
     
  14. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    And it does work ? o_O

    Can we have some screenshots ? The gameobjects which have the Collider/Rigibody and the script.
     
  15. MCamac55

    MCamac55

    Joined:
    Jan 8, 2015
    Posts:
    22
  16. MCamac55

    MCamac55

    Joined:
    Jan 8, 2015
    Posts:
    22
    Those are the screenshots (ignore the pause.s thing)
    Edit: It does not work :p
     
  17. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Okay, after few tests, it appears that one of your gameObject must have a Sphere Collider + Rigibody and the others should have at least a Collider.

    Edit: and that's what you did.. okay.
     
  18. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Did you check my edit:

    ? 'cause I don't really see what you have done wrong. I'm pretty sure it is something stupid that I don't see D:
     
  19. SevenHams

    SevenHams

    Joined:
    Dec 27, 2012
    Posts:
    67
    I think OnCollision or whatever that is called would be better than OnTriggerEnter
     
  20. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The distance check is better for a single pair. But if you are going to do this on multiple units the space partitioning done by the physics engine will make sphere triggers cheaper.
     
  21. MCamac55

    MCamac55

    Joined:
    Jan 8, 2015
    Posts:
    22

    I just redid the triggers and script and it works perfectly. Now to find out how to make the enemy follow the player