Search Unity

how do i make an enemy stop once it reaches my character?

Discussion in 'Animation' started by yaramaris95, Jul 23, 2019.

  1. yaramaris95

    yaramaris95

    Joined:
    May 27, 2019
    Posts:
    20
    Hello,

    I finaly got the running animation for my enemy to work, but the problem is that it wont stop running.

    the idea is that the enemy starts running to the player once the player is inside the circle where the enemy can see it. then once the enemy eighter reaches the player or the player goes out of sight the enemy needs to stop running and go back to the idle state. I have been trying and looking for answers for 4 hours now and i realy dont know how to fix this.

    this is the script i have on the enemy now

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class FindAndFollow : MonoBehaviour
    7. {
    8.     public float LookRadius = 100f;
    9.     Transform target;
    10.     NavMeshAgent agent;
    11.     Animator anim;
    12.  
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         target = PlayerManager.instance.player.transform;
    18.         agent = GetComponent<NavMeshAgent>();
    19.         anim = GetComponent<Animator>();
    20.    
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         float distance = Vector3.Distance(target.position, transform.position);
    27.  
    28.         if (distance <= LookRadius)
    29.         {
    30.             agent.SetDestination(target.position);
    31.             agent.isStopped = false;
    32.             anim.SetBool("IsRunning", true);
    33.          
    34.         }
    35.  
    36.         if ( distance <= agent.stoppingDistance)
    37.         {
    38.             FaceTarget();
    39.         }
    40.         if (distance >= LookRadius)
    41.         {
    42.             agent.isStopped = true;
    43.             anim.SetBool("IsRunning", false);
    44.         }
    45.  
    46.         if (LookRadius == 10f)
    47.         {
    48.             agent.isStopped = true;
    49.             anim.SetBool("IsRunning", false);
    50.         }
    51.  
    52.  
    53.     }
    54.  
    55.     void FaceTarget()
    56.     {
    57.         Vector3 direction = (target.position - transform.position).normalized;
    58.         Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
    59.         transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
    60.     }
    61.  
    62.  
    63.     void OnDrawGizmosSelected()
    64.     {
    65.         Gizmos.color = Color.red;
    66.         Gizmos.DrawWireSphere(transform.position, LookRadius);
    67.     }
    68. }
    i realy hope someone can help me fix this
     
  2. Riderfan

    Riderfan

    Joined:
    Jan 10, 2013
    Posts:
    514
    Wrong forum. This should go in scripting.
     
  3. yaramaris95

    yaramaris95

    Joined:
    May 27, 2019
    Posts:
    20
    oh sorry, im new so i didnt know.
     
  4. yahcheh_unity

    yahcheh_unity

    Joined:
    Aug 12, 2018
    Posts:
    1
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.AI;
    6.  
    7. public class FindAndFollow: MonoBehaviour
    8. {
    9.  
    10.     public float lookRadius = 5f;
    11.     NavMeshAgent agent;
    12.     Transform target;
    13.     [SerializeField] private Animator anim;
    14.     void Start()
    15.     {
    16.         target = PlayerManager.instance.player.transform;
    17.        agent = GetComponent<NavMeshAgent>();
    18.     }
    19.     void Update()
    20.     {
    21.  
    22.         float distance = Vector3.Distance(target.position, transform.position);
    23.         if (distance <= lookRadius)
    24.         {
    25.             anim.SetBool("isWalking", true);
    26.             agent.SetDestination(target.position);
    27.     }
    28.     else
    29.     {
    30.             anim.SetBool("isWalking", false);
    31.             agent.ResetPath();
    32.     }
    33.             if (distance <= 1f)
    34.         {
    35.             agent.SetBool("isWalking", false);
    36.             agent.ResetPath();
    37.             FaceTarget();
    38.         }
    39.     }
    40.             void FaceTarget ()
    41.         {
    42.             Vector3 direction = (target.position - transform.position).normalized;
    43.             Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
    44.             transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
    45.         }
    46.     void OnDrawGizmosSelected ()
    47.     {
    48.         Gizmos.color = Color.red;
    49.         Gizmos.DrawWireSphere(transform.position, lookRadius);
    50.     }
    51. }
    52.  
    53.  
    Try this. :)
     
    Last edited: Jan 2, 2022
  5. Oir_the_tentacular

    Oir_the_tentacular

    Joined:
    Nov 26, 2021
    Posts:
    76
    You could have your movement be enabled only while a boolean canMove = true. Then you could have a collision with the player set canMove = false.