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 Chase Range Help

Discussion in 'Scripting' started by DarkWolfSoftware, Aug 20, 2015.

  1. DarkWolfSoftware

    DarkWolfSoftware

    Joined:
    Jun 4, 2015
    Posts:
    31
    Hey guys, I am developing a Top Down Rpg and have been working on the combat mechanics of the game. I have run into a bit of a problem maybe you guys could help me with, I am sure it is just a small syntax I missed but it happens.

    The problem is my chase code, I am not sure if the problem persists in the actual script or in the inspector itself but the opponent chases the player regardless of the amount of range between the opponent and the player. In other words, I want the opponent to chase player only when the player is within a certain amount of range of the opponent but the opponent immediately begins to chase player as soon as the scene loads.

    Here is my sample code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Mob : MonoBehaviour {
    5.  
    6.     public float speed;
    7.     public float range;
    8.     public CharacterController controller;
    9.     public Transform player;
    10.  
    11.     public AnimationClip run;
    12.     public AnimationClip idle;
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.    
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.  
    22.         if (!inRange ()) {
    23.  
    24.             chase ();
    25.    
    26.         } else {
    27.  
    28.             GetComponent<Animation>().CrossFade (idle.name);
    29.  
    30.         }
    31.  
    32.     }
    33.  
    34.     bool inRange()
    35.     {
    36.         if (Vector3.Distance (transform.position, player.position) < range) {
    37.             return true;
    38.  
    39.         } else {
    40.  
    41.             return false;
    42.         }
    43.  
    44.     }
    45.  
    46.         void chase()
    47.         {
    48.  
    49.         transform.LookAt (player.position);
    50.         controller.SimpleMove (transform.forward * speed);
    51.         GetComponent<Animation>().CrossFade (run.name);
    52.     }
    53.  
    54.     void OnMouseOver(){
    55.         player.GetComponent<Combat> ().opponent = gameObject;
    56.     }
    57. }
    58.  
    Thanks in advance.
     
  2. Defero

    Defero

    Joined:
    Jul 9, 2012
    Posts:
    200
    Maybe if you remove the exclamation mark on line 22?

    if(inRange ()){
     
  3. Yarbius

    Yarbius

    Joined:
    Apr 29, 2014
    Posts:
    22
    You are chasing if not in range.