SHARE
TWEET

Untitled

a guest Jan 26th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. using UnityEngine.UI;
  6.  
  7. public class PatrolScript : MonoBehaviour
  8. {
  9.     public NavMeshAgent cop;
  10.     public GameObject Player;
  11.     public float FollowDistance = 20.0f;
  12.     public Transform[] patrolPoints;
  13.     private int currentPoint = 0;
  14.     public Vector3 jailPos;
  15.     public GameObject eye;
  16.     public NavMeshAgent doggy;//
  17.     void Start()
  18.     {
  19.         MoveToNextPoint();
  20.     }
  21.  
  22.     void Update()
  23.     {
  24.         float dist = Vector3.Distance(Player.transform.position, cop.transform.position);
  25.         float dogDist = Vector3.Distance(doggy.transform.position, cop.transform.position);//
  26.         bool patrol = false;
  27.         bool runAway = false;//
  28.         bool follow = (dist < FollowDistance && !runAway);
  29.         if (follow)
  30.         {
  31.             cop.SetDestination(Player.transform.position);
  32.             eye.SetActive(true);
  33.             if(dist <= 1.05f)
  34.             {
  35.                 sendToJail();
  36.             }
  37.         }
  38.  
  39.         patrol = !follow && !runAway;
  40.  
  41.         if (!follow && !patrol && !runAway)//
  42.         {
  43.             eye.SetActive(false);
  44.             patrol = true;
  45.           //  cop.SetDestination(patrolPoints[currentPoint].position);
  46.         }
  47.         if (patrol)
  48.         {
  49.        eye.SetActive(false);
  50.  
  51.             if (!cop.pathPending && cop.remainingDistance < 0.5f)
  52.             {
  53.                 MoveToNextPoint();
  54.             }
  55.         }
  56.         if (runAway)//
  57.         {
  58.             patrol = false;
  59.             follow = false;
  60.             Vector3 moveDir = cop.transform.position - doggy.transform.position;
  61.  
  62.             transform.Translate(moveDir.normalized * 30 * Time.deltaTime);
  63.             if (dogDist > 25 && dist > FollowDistance)
  64.             {
  65.                 patrol = true;
  66.                 runAway = false;
  67.                 follow = false;
  68.             }else if ( dogDist > FollowDistance)  {
  69.                 patrol = false;
  70.                 runAway = false;
  71.                 follow = true;
  72.             }
  73.         }
  74.     }
  75.  
  76.     void MoveToNextPoint()
  77.     {
  78.         cop.destination = patrolPoints[currentPoint].position;
  79.         currentPoint++;
  80.         if (currentPoint >= patrolPoints.Length)
  81.         {
  82.             currentPoint = 0;
  83.         }
  84.     }
  85.     void sendToJail()
  86.     {
  87.         Player.transform.position = jailPos;
  88.         PlayerPrefs.SetInt("money", PlayerPrefs.GetInt("money") - 100);
  89.         PlayerPrefs.SetInt("points", 0);
  90.         //clear inv
  91.     }
  92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top