Advertisement
RickyDean

Untitled

Apr 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 KB | None | 0 0
  1.  using UnityEngine;
  2.  using System.Collections;
  3.  using UnityEngine.AI;
  4.  
  5.  public class Wander : MonoBehaviour
  6.  {
  7.      [SerializeField]
  8.      float Speed = 20;
  9.  
  10.      Vector3 wayPoint;
  11.  
  12.      [SerializeField]
  13.      int Range = 10;
  14.  
  15.      void Start()
  16.      {
  17.          //initialise the target way point
  18.          wander();
  19.      }
  20.  
  21.      void Update()
  22.      {
  23.          // this is called every frame
  24.          // do move code here
  25.          transform.position += transform.TransformDirection(Vector3.forward) * Speed * Time.deltaTime;
  26.          if ((transform.position - wayPoint).magnitude < 3)
  27.          {
  28.              // when the distance between us and the target is less than 3
  29.              // create a new way point target
  30.             { Debug.Log("Working"); wander(); }
  31.              wander();
  32.  
  33.  
  34.          }
  35.      }
  36.  
  37. void wander() {
  38. wayPoint = Random.insideUnitSphere * Range ;
  39.  GetComponent<NavMeshAgent>().SetDestination(wayPoint);
  40. }
  41.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement