duck

Unity 3D Robot navmesh script step 4

Nov 14th, 2012
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Robot : MonoBehaviour
  5. {
  6.    
  7.     Transform target;
  8.    
  9.     public float seeDistance = 20;
  10.     public float fireRepeatSpeed = 4;
  11.     public float fireProjectileSpeed = 30;
  12.    
  13.     public Rigidbody projectile;
  14.     public LayerMask rayMask;
  15.    
  16.     NavMeshAgent agent;
  17.     Animation avatar;
  18.     Transform eyes;
  19.     bool canSeePlayer;
  20.     Vector3 delta;
  21.    
  22.    
  23.     // Use this for initialization
  24.     void Start ()
  25.     {
  26.         target = GameObject.FindGameObjectWithTag("Player").transform;
  27.         agent = GetComponent<NavMeshAgent> ();
  28.         avatar = GetComponentInChildren<Animation> ();
  29.         eyes = transform.Find ("Eyes");
  30.         StartCoroutine( Tracking() );
  31.         StartCoroutine( Firing () );
  32.     }
  33.    
  34.     IEnumerator Firing()
  35.     {
  36.         while (true)
  37.         {
  38.             yield return new WaitForSeconds(fireRepeatSpeed);
  39.            
  40.             if (canSeePlayer) {
  41.                 Fire();
  42.             }
  43.         }
  44.     }
  45.    
  46.    
  47.    
  48.     void Fire()
  49.     {
  50.         Rigidbody newProjectile = (Rigidbody) Instantiate ( projectile, eyes.position, eyes.rotation );
  51.         newProjectile.AddForce( delta.normalized * fireProjectileSpeed , ForceMode.Impulse );
  52.     }
  53.    
  54.    
  55.    
  56.    
  57.     IEnumerator Tracking ()
  58.     {
  59.         while (true) {
  60.             yield return new WaitForSeconds(1);
  61.            
  62.             delta = target.position - eyes.position;
  63.            
  64.             Ray ray = new Ray (eyes.position, delta);
  65.             RaycastHit hit;
  66.            
  67.             if (Physics.Raycast (ray, out hit, seeDistance, rayMask)) {
  68.                
  69.                 Debug.Log (hit.collider.name);
  70.                
  71.                 if (hit.collider.tag == "Player")
  72.                 {
  73.                     agent.SetDestination (target.position);
  74.                     canSeePlayer = true;
  75.                 } else {
  76.                     canSeePlayer = false;
  77.                 }
  78.                
  79.             }
  80.            
  81.         }
  82.        
  83.     }
  84.    
  85.    
  86.    
  87.     // Update is called once per frame
  88.     void Update ()
  89.     {
  90.        
  91.         if (agent.velocity.magnitude > 0.1f) {
  92.             avatar.CrossFade ("run");
  93.         } else {
  94.             avatar.CrossFade ("idle");
  95.         }
  96.        
  97.        
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment