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

Enemy character won't stop moving once they are within a specific range.

Discussion in 'Scripting' started by RayDawg, Mar 23, 2015.

  1. RayDawg

    RayDawg

    Joined:
    Mar 19, 2013
    Posts:
    108
    I have a script that detects when the player is nearby and moves an enemy character to the player. However, when the enemy character is within range, the character is supposed to stop moving, but doesn't. Could someone tell me what I did wrong? ("ThisObject" is the enemy object that has the script attached to it)


    Code (CSharp):
    1.     void FixedUpdate()
    2.     {
    3.         float DistanceDifference = Vector3.Distance(Player.gameObject.transform.position, ThisObject.position);    // Difference between the player and this object
    4.        //If the distance is less than the detection range, which is effectively the aggro range
    5.         if (DistanceDifference < DetectionRange)
    6.         {
    7.             // Change the gameobject's color to yellow to indicate aggro has been achieved
    8.             gameObject.GetComponent<Renderer>().material.color = Color.red;
    9.             // If the distance difference is greater than melee range
    10.             if (DistanceDifference > MeleeRange)
    11.             {
    12.                 // Rotate to player
    13.                 ThisObject.rotation = Quaternion.Slerp(ThisObject.transform.rotation, Quaternion.LookRotation(Player.transform.position - ThisObject.transform.position), RotateSpeed * Time.deltaTime);
    14.  
    15.                 // Move to player
    16.                 ThisObject.position += ThisObject.forward * MoveSpeed * Time.deltaTime;
    17.             }
    18.  
    19.             else if (DistanceDifference <= MeleeRange)
    20.             {
    21.                 // Timer will store the time.time inside this if statement
    22.                 if (Time.time > timer + AttacksPerSecond)
    23.                 {
    24.                     // Attack at an interval
    25.                     Player.gameObject.GetComponent<PlayerStatistics>().TakeDamage(Damage);
    26.                     Debug.Log(DistanceDifference);
    27.                     timer = Time.time;
    28.                 }
    29.             }
    30.         }
    31.     }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Nothing stands out to me -- what's MeleeRange set to? Toss some more Debug.Log() statements in to help narrow it down as well.
     
  3. RayDawg

    RayDawg

    Joined:
    Mar 19, 2013
    Posts:
    108
    MeleeRange is set to 5.0f, DetectionRange is set to 20.0f, Debug.Log() shows all things working normally, but I suppose it's something I overlooked?

    Edit: So after placing boolean around, it still didn't work, however, I did notice something weird:

    Debug statements indicate that the if statement checking if DistanceDifference <= MeleeRange only trigger when the enemy Object collides with the player character.
     
    Last edited: Mar 24, 2015
  4. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    When you say Debug.Log() shows all things working normally what do you mean?

    Are you sure that your enemy is actually getting within a distance of 5 units? If you haven't already you can verify this with Debug messages reporting the DistanceDifference every frame
     
  5. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Depending on the scale of your assets 5m could look like 5 inches. It's possible that 5m just isn't far enough away. Try increasing detection to 50 and MeleeRange to something like 20. If that solves it you'll just need to scale down your assets to the appropriate size. Remember that the average person is roughly 2m tall. Unity's primitive Capsule is this height I believe so you can use that for reference.

    Side note for bonus points: You're using Slerp incorrectly. What you want to be using is SmoothDamp for that effect.