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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do You Script Enemies following you upon Seeing you

Discussion in 'Scripting' started by greatUnityGamer, Feb 20, 2015.

  1. greatUnityGamer

    greatUnityGamer

    Joined:
    Aug 11, 2013
    Posts:
    182
    In an FPS game i want enemies to start following me(running towards me or walking with the intention of attacking me) upon seeing me.

    How would i do this?....
    They would then want to keep following me and attacking me(punches, kicks etc) no matter what position and direction i go to. How would they repeatedly get player's position and orientation.

    And how do i make the enemies find out how far or close they are to the player and say something like.
    "if your this close to him...stop and attack"
     
  2. Aidy

    Aidy

    Joined:
    Oct 15, 2014
    Posts:
    19
    If you want them to have a line of sight and then wait until the player comes into that line of sight, you'll want to use a raycast. Raycasts are fairly straightforward for that, and there's Unity tutorial for that.

    As for judging the distance, there're a couple ways you can go about doing that depending on what you already have. I've done it by using a Physics2D.OverlapCircleAll (because my current project is in 2D, but the 3D alternative is Physics.OverlapSpehre and it's the exact same process). I'll show you here:

    Code (CSharp):
    1.     private void Attack()
    2.     {
    3.         //checks the facing direction of to the right of the player (equal to Vector3(1,0,0))
    4.         Vector3 facingDirection = Vector3.right;
    5.  
    6.         //checks to see if there are any enemies within an invisible overlap circle projected in front of the player at a predefined size, and if so, it adds it into the array which is checked below
    7.         Collider2D [] enemyColliders = Physics2D.OverlapCircleAll (myTransform.position + facingDirection * 1, 1, 1 << 8);
    8.  
    9.         //so as long as there are enemies existing in the array, then they can be damaged, it prevents enemies being invincible and prevents the player dealing damage to enemies that they can't actually reach
    10.         for (int i = 0; i < enemyColliders.Length; ++i)                                  
    11.         {
    12.             Debug.Log(i);
    13.             enemyColliders[i].GetComponent<EnemyScript>().TakeDamage(1);   //the enemies in the array takesdamage
    14.         }
    15.     }
    Alternatively, it can be done simply through checking Vector3.Distance, and BurgZerg Arcade has a great series of tutorials for all of that stuff, specifically he has one here with a simple check for distance (he replaces it with a more advanced one later in the series).
     
    OboShape likes this.