SHARE
TWEET

Untitled

a guest Aug 8th, 2017 2,778 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.  
  6. public class Ninja : MonoBehaviour {
  7.  
  8.     public float range;
  9.     public float speed;
  10.     public NavMeshAgent nav;
  11.     float timer = 0;
  12.     public float attackSpeed;
  13.  
  14.     public GameObject projectile;
  15.     public GameObject spawner;
  16.  
  17.     GameObject target;
  18.     public GameObject blood;
  19.  
  20.  
  21.     void Start () {
  22.         nav.speed = Random.Range (1f, 2f);
  23.         GameObject[] cowboys = GameObject.FindGameObjectsWithTag("cowboy");
  24.         float minDist = Mathf.Infinity;
  25.         foreach (GameObject cowboy in cowboys) {
  26.             float dist = Vector3.Distance (this.transform.position, cowboy.transform.position);
  27.             if (dist < minDist){
  28.                 target = cowboy;
  29.                 minDist = dist;
  30.             }
  31.         }
  32.     }
  33.    
  34.     // Update is called once per frame
  35.     void Update () {
  36.         if (target != null){
  37.             timer -= Time.deltaTime;
  38.             if (Vector3.Distance (this.transform.position, target.transform.position) > range) {
  39.                 nav.destination = target.transform.position;
  40.             } else {
  41.                 Vector3 relPos = target.transform.position - this.transform.position;
  42.                 this.transform.rotation = Quaternion.LookRotation (relPos);
  43.  
  44.                 nav.destination = this.transform.position;
  45.                 if (timer <= 0){
  46.                     Attack ();
  47.                     timer = attackSpeed;
  48.                 }
  49.             }
  50.         }
  51.     }
  52.  
  53.     void OnCollisionEnter(Collision obj){
  54.         if (obj.transform.tag == "bullet"){
  55.             Death ();
  56.         }
  57.  
  58.     }
  59.  
  60.     void Death(){
  61.         Instantiate (blood, this.transform.position, this.transform.rotation);
  62.         GameObject.Find ("Manager").GetComponent<Manager> ().CashIn (10);
  63.         Destroy (this.gameObject);
  64.     }
  65.  
  66.     void Attack(){
  67.         GameObject star = Instantiate (projectile,spawner.transform.position, this.transform.rotation);
  68.         star.GetComponent<Projectile>().target = target.transform.position;
  69.  
  70.     }
  71. }
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