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

Animal AI random movements

Discussion in 'Scripting' started by casperround, Feb 28, 2015.

  1. casperround

    casperround

    Joined:
    Feb 24, 2015
    Posts:
    2
    Hi, I'm very new to Unity3D but have got the hang of the basic map design and animations etc, how ever I can't find any tutorials anywhere which follow similar lines to animating animals to have random movements, such as hostile mobs.

    Could anyone either tell me what I could do or point me to a Tutorial.

    Thanks
     
  2. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    casperround likes this.
  3. casperround

    casperround

    Joined:
    Feb 24, 2015
    Posts:
    2
    Thank you so much!, it worked!!!
     
  4. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    Think about it, you need the animal to move to a random position, then maybe wait for some time and then move again. While doing these, you need it to animate. So, you first need to figure out how to move the animals. Couple of ways :

    - Translate Movement / Move Towards Function / Linear Interpolation
    - Navmesh Agent Movement
    - Rigidbody Movement

    I would suggest Translate Movement, Lerp for now, actually after I tell you the logic and give some code, it won't differ too much how you make the movement, the tricky part is to actually pick the movement position. Since all of these movement types require a position, then it won't be a problem to change the movement type. Now you need a random position right ? So we need to define a radius for it. Again, couple of ways :

    - There is something called Random.insideUnitCircle, it is a Vector2 and it picks random position in X and Y axis. You can use this to create a random position.
    - Manually creating your random position by Random.Range.

    I'll go with the second one. Another thing is, if you want your animals to wait for some time when a random position is reached, you can use a coroutine for creating a delay.


    Code (CSharp):
    1. public float speed;
    2. public float randomX;
    3. public float randomZ;
    4. public float minWaitTime;
    5. public float maxWaitTime;
    6. private Vector3 currentRandomPos;
    7.  
    8.  
    9. void Start()
    10. {
    11. PickPosition();
    12. }
    13.  
    14. void PickPosition()
    15. {
    16. currentRandomPos = new Vector3(Random.Range(-randomX, randomX), 0, Random.Range(-randomZ, randomZ);
    17. StartCoroutine ( MoveToRandomPos());
    18.  
    19. }
    20.  
    21. IEnumerator MoveToRandomPos()
    22. {
    23. float i = 0.0f;
    24. float rate = 1.0f / speed;
    25. Vector3 currentPos = transform.position;
    26.  
    27. while (i < 1.0f)
    28. {
    29. i += Time.deltaTime * rate;
    30. transform.position = Vector3.Lerp( currentPos, currendRandomPos, i);
    31. yield return null;
    32. }
    33.  
    34. float randomFloat = Random.Range(0.0f,1.0f); // Create %50 chance to wait
    35. if(randomFloat < 0.5f)
    36. StartCoroutine ( WaitForSomeTime());
    37. else
    38. PickPosition();
    39. }
    40.  
    41. IEnumerator WaitForSomeTime()
    42. {
    43. yield return new WaitForSeconds(Random.Range(minWaitTime, maxWaitTime);
    44. PickPosition();
    45. }
    You can also search & learn Animator, and set it's parameters while moving, waiting and create your animations by it.
    Hope that the logic is helpful, have a nice day !
     
  5. harshie55

    harshie55

    Joined:
    Sep 7, 2018
    Posts:
    4
    Hi casper!

    I'm a complete beginner as well at this and was wondering if you could help me out for a minute? Do I simply download the wander script and attach it to an animal GameObject in my scene to make it wander randomly?

    Thanks
     
  6. adarshabn

    adarshabn

    Joined:
    Sep 7, 2018
    Posts:
    5
     
  7. MysticInteractive

    MysticInteractive

    Joined:
    May 13, 2018
    Posts:
    2
    This didn't seem to work for me... Here's my script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AnimalMovement : MonoBehaviour
    6. {
    7.     Animator anim;
    8.  
    9.     [SerializeField]
    10.     private float speed;
    11.     [SerializeField]
    12.     private float randomX;
    13.     [SerializeField]
    14.     private float randomZ;
    15.     [SerializeField]
    16.     private float minWaitTime;
    17.     [SerializeField]
    18.     private int maxWaitTime;
    19.  
    20.     private Vector3 currentRandomPos;
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         speed -= anim.GetFloat("Speed");
    26.  
    27.         anim = GetComponent<Animator>();
    28.  
    29.         PickPosition();
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update()
    34.     {
    35.  
    36.     }
    37.  
    38.     void PickPosition()
    39.     {
    40.         currentRandomPos = new Vector3(Random.Range(-randomX, randomX), 0, Random.Range(-randomZ, randomZ));
    41.         StartCoroutine(MoveToRandomPos());
    42.     }
    43.  
    44.     IEnumerator MoveToRandomPos()
    45.     {
    46.         float i = 0.0f;
    47.         float rate = 1.0f / speed;
    48.         Vector3 currentPos = transform.position;
    49.  
    50.         while (i < 1.0f)
    51.         {
    52.             i += Time.deltaTime * rate;
    53.             transform.position = Vector3.Lerp(currentPos, currentRandomPos, i);
    54.             yield return null;
    55.         }
    56.  
    57.         float randomFloat = Random.Range(0.0f, 1.0f);
    58.        
    59.         if(randomFloat < 0.5f)
    60.         {
    61.             StartCoroutine(WaitForSomeTime());
    62.         }
    63.  
    64.         IEnumerator WaitForSomeTime()
    65.         {
    66.             yield return new WaitForSeconds(Random.Range(minWaitTime, maxWaitTime));
    67.             PickPosition();
    68.         }
    69.     }
    70. }
    Do you see any problems in it? If you do, please let me know. Also, does anybody know how I can use one animation controller for every animal (the animals are made myself using the "animation" window.)
     
  8. UNITYPRO750

    UNITYPRO750

    Joined:
    Jun 21, 2020
    Posts:
    1
  9. McPeppergames

    McPeppergames

    Joined:
    Feb 15, 2019
    Posts:
    103
    I wonder if there is a special NavMeshAgent for animals and non-human forms?
     
  10. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,868
    No. NavMeshAgents are very generic, very low level scripts, they aren't show any behavior other than following a path. So why would they have different?