Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.AI;
- public class Ninja : MonoBehaviour {
- public float range;
- public float speed;
- public NavMeshAgent nav;
- float timer = 0;
- public float attackSpeed;
- public GameObject projectile;
- public GameObject spawner;
- GameObject target;
- public GameObject blood;
- void Start () {
- nav.speed = Random.Range (1f, 2f);
- GameObject[] cowboys = GameObject.FindGameObjectsWithTag("cowboy");
- float minDist = Mathf.Infinity;
- foreach (GameObject cowboy in cowboys) {
- float dist = Vector3.Distance (this.transform.position, cowboy.transform.position);
- if (dist < minDist){
- target = cowboy;
- minDist = dist;
- }
- }
- }
- // Update is called once per frame
- void Update () {
- if (target != null){
- timer -= Time.deltaTime;
- if (Vector3.Distance (this.transform.position, target.transform.position) > range) {
- nav.destination = target.transform.position;
- } else {
- Vector3 relPos = target.transform.position - this.transform.position;
- this.transform.rotation = Quaternion.LookRotation (relPos);
- nav.destination = this.transform.position;
- if (timer <= 0){
- Attack ();
- timer = attackSpeed;
- }
- }
- }
- }
- void OnCollisionEnter(Collision obj){
- if (obj.transform.tag == "bullet"){
- Death ();
- }
- }
- void Death(){
- Instantiate (blood, this.transform.position, this.transform.rotation);
- GameObject.Find ("Manager").GetComponent<Manager> ().CashIn (10);
- Destroy (this.gameObject);
- }
- void Attack(){
- GameObject star = Instantiate (projectile,spawner.transform.position, this.transform.rotation);
- star.GetComponent<Projectile>().target = target.transform.position;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement