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. Dismiss Notice

Wandering AI Rotation

Discussion in 'Scripting' started by Tibsterx, Mar 25, 2021.

  1. Tibsterx

    Tibsterx

    Joined:
    Mar 4, 2020
    Posts:
    10
    Hey there! I am trying to implement the wandering AI steering behavior, but I am having a problem with the rotations. Here is my code:
    Code (CSharp):
    1. static int i;
    2.     private static void AIWander(Rigidbody _rb) {
    3.         //Add a small randomness to the targets position  
    4.         float frameJitter = wanderJitter * Time.deltaTime;
    5.  
    6.         if (i >= 50) {
    7.             //Only change direction every fifty updates
    8.             wanderTarget += new Vector3((UnityEngine.Random.Range(-1f, 1f) * frameJitter), 0, (UnityEngine.Random.Range(-1f, 1f) * frameJitter));
    9.             wanderTarget.Normalize();
    10.             wanderTarget *= wanderRadius;
    11.             i = 0;
    12.         }
    13.  
    14.         //Move the target in front of the ai
    15.         Vector3 targetLocal = new Vector3(wanderTarget.x, 0, wanderTarget.y + wanderDistance);
    16.         Vector3 targetWorld = _rb.transform.TransformPoint(targetLocal);
    17.  
    18.         //Apply
    19.         AISeek(_rb, targetWorld);
    20.         i++;
    21.     }
    22.  
    23. private static void AISeek(Rigidbody _rb, Vector3 _targetPosition) {
    24.         //Find the direction towards the target and proceed to move that way
    25.         Vector3 desiredVelocity = ((_targetPosition - _rb.position).normalized) * aiSpeed;
    26.  
    27.         //Apply
    28.         _rb.MovePosition(_rb.transform.position + new Vector3(desiredVelocity.x, 0, desiredVelocity.z) * Time.deltaTime);
    29.         _rb.transform.LookAt(new Vector3(_targetPosition.x, _rb.position.y, _targetPosition.z));
    30.     }
    31.  
    I know that my problem stems from me setting the local target to world using the rigidbody in Wander, but then rotating that rigidbody in seek every udate. How can I get the ai to still look where it is going while still incrementing the wander correctly? Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    This is too vague. Is it not turning at all? Is it spazzing out? Is it turning the wrong way? Consistently the wrong way? Only sometimes the wrong way? When is it wrong? etc. etc. ec.

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
  3. Tibsterx

    Tibsterx

    Joined:
    Mar 4, 2020
    Posts:
    10
    Sorry, I clarified a bit more at the bottom of the post but here is the problem. In AI Wander I am first modifying the target locally and then setting its world position. I've solved it now anyways, thanks.
     
    Kurt-Dekker likes this.