Search Unity

Enemy wandering randomly in a certain range

Discussion in 'Scripting' started by wwewef, Mar 1, 2015.

  1. wwewef

    wwewef

    Joined:
    Dec 29, 2014
    Posts:
    2
    Hi: Im working on a FPS mixed with some survival in 3D. I have an animated model that represents my enemy. I can make him move towards me if I am in range. If he gets closer enough he stops moving(and animating). Now Im trying to achieve a Wandering behaviour.

    My idea goes like this: The enemy starts in the start position. He checks if the player is in range, if true then FollowPlayer() if false then MoveAround(), this means that he calculate a random position in a certain range of his actual position, Slerp to it and then move to that random position. After reaching the random position he should wait X seconds until the next move . The MoveAround() method I have is a Coroutine but I cant make it work. Here is the script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyAI : MonoBehaviour {
    5.  
    6.     public float rotationSpeed;
    7.     public float moveSpeed;
    8.     public float maxSpeed;
    9.     public float minRange;
    10.     public float maxRange;
    11.     public float moveRange;
    12.     Animator anim;
    13.     CharacterController controller;
    14.  
    15.     // Use this for initialization
    16.     void Awake (){
    17.         anim = GetComponent<Animator> ();
    18.         controller = GetComponent<CharacterController> ();
    19.     }
    20.     void Start () {
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void FixedUpdate () {
    25.         FollowPlayer ();
    26.         StartCoroutine(MoveAround ());
    27.     }
    28.     void FollowPlayer(){
    29.         Vector3 playerPos = GameObject.Find ("Player").transform.position;
    30.         Vector3 lookDir = playerPos - transform.position;
    31.         Vector3 moveDir = lookDir;// * moveSpeed;
    32.         moveDir *= Time.fixedDeltaTime;
    33.  
    34.         if((Vector3.Distance(transform.position, playerPos) <= maxRange) && (Vector3.Distance(transform.position, playerPos) > minRange) ){
    35.             Vector3 previous = transform.position;
    36.             transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (lookDir), rotationSpeed * Time.fixedDeltaTime);
    37.             controller.Move(moveDir);
    38.             float velocity = ((transform.position - previous).magnitude) / Time.fixedDeltaTime;
    39.         //    Debug.Log("velocidad: " + velocity);
    40.             previous = transform.position;
    41.             anim.SetFloat ("speed", velocity);
    42.         }
    43.         if(Vector3.Distance(transform.position, playerPos) < minRange){
    44.             controller.Move(Vector3.zero);
    45.             Debug.LogWarning("hey");
    46.             anim.SetFloat("speed",controller.velocity.magnitude);
    47.         }
    48.     }
    49.     IEnumerator MoveAround(){
    50.         Vector3 playerPos = GameObject.Find ("Player").transform.position;
    51.         Vector3 randomPos = Random.onUnitSphere * moveRange;
    52.         randomPos = new Vector3 (randomPos.x + transform.position.x, transform.position.y, randomPos.z + transform.position.z);
    53.         Vector3 lookDir = randomPos - transform.position;
    54.         Vector3 moveDir = lookDir;
    55.         moveDir *= Time.fixedDeltaTime;
    56.         Debug.Log ("Player Pos: " + playerPos);
    57.         Debug.Log ("Random Pos: " + randomPos);
    58.         Debug.Log ("Look Dir: " + lookDir);
    59.         Debug.Log ("Move Dir: " + moveDir);
    60.  
    61.         if(Vector3.Distance(transform.position, playerPos) > maxRange){
    62.             Debug.Log("Moving the enemy");
    63.             Vector3 previous = transform.position;
    64.             transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (lookDir), rotationSpeed * Time.fixedDeltaTime);
    65.             controller.Move(moveDir);
    66.             float velocity = ((transform.position - previous).magnitude) / Time.fixedDeltaTime;
    67.         //    Debug.Log("velocidad: " + velocity);
    68.             previous = transform.position;
    69.             anim.SetFloat ("speed", velocity);
    70.             Debug.Log("Enemy moved");
    71.             yield return new WaitForSeconds(2f);
    72.             Debug.Log("esperando primeros 2 segundos");
    73.         }
    74.         Debug.Log ("he llegado al destino");
    75.         yield return new WaitForSeconds(2f);
    76.         Debug.Log ("COroutine final");
    77.     }
    78.  
    79.     void OnDrawGizmosSelected(){
    80.         Gizmos.color = Color.green;
    81.         Gizmos.DrawWireSphere (transform.position, maxRange);
    82.         Gizmos.DrawWireSphere (transform.position, minRange);
    83.     }
    84. }
    85.