Advertisement
mvaganov

GhostMover.cs

Feb 19th, 2017
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(Rigidbody))]
  6. public class GhostMover : MonoBehaviour {
  7.  
  8.     public Vector3 nextPlaceToGo;
  9.     public float timerSeconds = 1;
  10.     private float timePassed;
  11.     bool chasing = false;
  12.     public float speed = 2f;
  13.  
  14.     public int hitPoints = 1;
  15.  
  16.     public void LaserHit()
  17.     {
  18.         Debug.Log ("I've been hit by a lazer");
  19.         hitPoints--;
  20.         if (hitPoints <= 0) {
  21.             GameObject.Destroy (gameObject);
  22.         }
  23.     }
  24.  
  25.     void Start() {
  26.         GetComponent<Rigidbody> ().useGravity = false;
  27.  
  28.     }
  29.  
  30.     GameObject lookLine;// <-- used to draw math. get Lines.cs from // http://pastebin.com/8m69iTut
  31.  
  32.     private float killDistance = 1.5f;
  33.  
  34.     void FixedUpdate () {
  35.         float distance = Vector3.Distance( transform.position, GameObject.FindWithTag("Player").transform.position);
  36.         //Debug.Log (distance);
  37.         if ( distance <= killDistance)
  38.             {
  39.                 Application.LoadLevel(Application.loadedLevel);
  40.             }
  41.            
  42.         timePassed += Time.deltaTime;
  43.         if (timePassed >= timerSeconds) {
  44.             if (!chasing) {
  45.                 Vector3 randomOnPlane = new Vector3 (
  46.                     Random.Range(-1f,1f), 0, Random.Range(-1f,1f));
  47.                 nextPlaceToGo = transform.position + randomOnPlane.normalized;
  48.             }
  49.             timePassed = 0;
  50.         }
  51.         Vector3 actualDirection = nextPlaceToGo - transform.position;
  52.         GetComponent<Rigidbody> ().velocity = actualDirection.normalized * speed;
  53.         transform.rotation = Quaternion.LookRotation (actualDirection.normalized);
  54.         Ray ray = new Ray (transform.position, actualDirection);
  55.         // this will draw lines. get Lines.cs from http://pastebin.com/8m69iTut
  56.         Lines.Make (ref lookLine, transform.position, transform.position + ray.direction);
  57.         RaycastHit hit = new RaycastHit ();
  58.         if (Physics.Raycast (ray, out hit)) {
  59.             if (hit.collider.tag == "Player") {
  60.                 Debug.Log ("SAW PLAYER!");
  61.                 //timePassed = 0;
  62.                 nextPlaceToGo = hit.collider.transform.position;
  63.                 chasing = true;
  64. //              transform.LookAt (hit.collider.transform.position);
  65. //              GetComponent<Rigidbody> ().angularVelocity = new Vector3();
  66.             } else {
  67.                 chasing = false;
  68.             }
  69.         }
  70.  
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement