Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(Rigidbody))]
- public class GhostMover : MonoBehaviour {
- public Vector3 nextPlaceToGo;
- public float timerSeconds = 1;
- private float timePassed;
- bool chasing = false;
- public float speed = 2f;
- public int hitPoints = 1;
- public void LaserHit()
- {
- Debug.Log ("I've been hit by a lazer");
- hitPoints--;
- if (hitPoints <= 0) {
- GameObject.Destroy (gameObject);
- }
- }
- void Start() {
- GetComponent<Rigidbody> ().useGravity = false;
- }
- GameObject lookLine;// <-- used to draw math. get Lines.cs from // http://pastebin.com/8m69iTut
- private float killDistance = 1.5f;
- void FixedUpdate () {
- float distance = Vector3.Distance( transform.position, GameObject.FindWithTag("Player").transform.position);
- //Debug.Log (distance);
- if ( distance <= killDistance)
- {
- Application.LoadLevel(Application.loadedLevel);
- }
- timePassed += Time.deltaTime;
- if (timePassed >= timerSeconds) {
- if (!chasing) {
- Vector3 randomOnPlane = new Vector3 (
- Random.Range(-1f,1f), 0, Random.Range(-1f,1f));
- nextPlaceToGo = transform.position + randomOnPlane.normalized;
- }
- timePassed = 0;
- }
- Vector3 actualDirection = nextPlaceToGo - transform.position;
- GetComponent<Rigidbody> ().velocity = actualDirection.normalized * speed;
- transform.rotation = Quaternion.LookRotation (actualDirection.normalized);
- Ray ray = new Ray (transform.position, actualDirection);
- // this will draw lines. get Lines.cs from http://pastebin.com/8m69iTut
- Lines.Make (ref lookLine, transform.position, transform.position + ray.direction);
- RaycastHit hit = new RaycastHit ();
- if (Physics.Raycast (ray, out hit)) {
- if (hit.collider.tag == "Player") {
- Debug.Log ("SAW PLAYER!");
- //timePassed = 0;
- nextPlaceToGo = hit.collider.transform.position;
- chasing = true;
- // transform.LookAt (hit.collider.transform.position);
- // GetComponent<Rigidbody> ().angularVelocity = new Vector3();
- } else {
- chasing = false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement