Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Robot : MonoBehaviour
- {
- Transform target;
- public float seeDistance = 20;
- public float fireRepeatSpeed = 4;
- public float fireProjectileSpeed = 30;
- public Rigidbody projectile;
- public LayerMask rayMask;
- NavMeshAgent agent;
- Animation avatar;
- Transform eyes;
- bool canSeePlayer;
- Vector3 delta;
- // Use this for initialization
- void Start ()
- {
- target = GameObject.FindGameObjectWithTag("Player").transform;
- agent = GetComponent<NavMeshAgent> ();
- avatar = GetComponentInChildren<Animation> ();
- eyes = transform.Find ("Eyes");
- StartCoroutine( Tracking() );
- StartCoroutine( Firing () );
- }
- IEnumerator Firing()
- {
- while (true)
- {
- yield return new WaitForSeconds(fireRepeatSpeed);
- if (canSeePlayer) {
- Fire();
- }
- }
- }
- void Fire()
- {
- Rigidbody newProjectile = (Rigidbody) Instantiate ( projectile, eyes.position, eyes.rotation );
- newProjectile.AddForce( delta.normalized * fireProjectileSpeed , ForceMode.Impulse );
- }
- IEnumerator Tracking ()
- {
- while (true) {
- yield return new WaitForSeconds(1);
- delta = target.position - eyes.position;
- Ray ray = new Ray (eyes.position, delta);
- RaycastHit hit;
- if (Physics.Raycast (ray, out hit, seeDistance, rayMask)) {
- Debug.Log (hit.collider.name);
- if (hit.collider.tag == "Player")
- {
- agent.SetDestination (target.position);
- canSeePlayer = true;
- } else {
- canSeePlayer = false;
- }
- }
- }
- }
- // Update is called once per frame
- void Update ()
- {
- if (agent.velocity.magnitude > 0.1f) {
- avatar.CrossFade ("run");
- } else {
- avatar.CrossFade ("idle");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment