Advertisement
OwlyOwl

guard_patrol

May 1st, 2020
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Patrol : MonoBehaviour
  6. {
  7.     public float speed;
  8.     private float waitTime;
  9.     public float startWaitTime;
  10.  
  11.     public Transform[] moveSpots;
  12.     private int randomSpot;
  13.  
  14.     void Start()
  15.     {
  16.         waitTime = startWaitTime;
  17.         randomSpot = Random.Range(0, moveSpots.Length);
  18.     }
  19.  
  20.     void Update()
  21.     {
  22.         transform.position = Vector2.MoveTowards(transform.position, moveSpots[randomSpot].position, speed * Time.deltaTime);
  23.         if (Vector2.Distance(transform.position, moveSpots[randomSpot].position) < 0.3f)
  24.         {
  25.             if (waitTime <= 0)
  26.             {
  27.                 randomSpot = Random.Range(0, moveSpots.Length);
  28.                 waitTime = startWaitTime;
  29.             }
  30.             else
  31.             {
  32.                 waitTime -= Time.deltaTime;
  33.             }
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement