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;
- NavMeshAgent agent;
- Animation avatar;
- Transform eyes;
- // Use this for initialization
- void Start ()
- {
- target = GameObject.FindGameObjectWithTag("Player").transform;
- agent = GetComponent<NavMeshAgent> ();
- avatar = GetComponentInChildren<Animation> ();
- eyes = transform.Find ("Eyes");
- StartCoroutine( Tracking() );
- }
- IEnumerator Tracking ()
- {
- while (true) {
- yield return new WaitForSeconds(1);
- Vector3 delta = target.position - eyes.position;
- Ray ray = new Ray (eyes.position, delta);
- RaycastHit hit;
- if (Physics.Raycast (ray, out hit, seeDistance)) {
- Debug.Log( hit.collider.name );
- }
- }
- }
- // Update is called once per frame
- void Update ()
- {
- agent.SetDestination (target.position);
- if (agent.velocity.magnitude > 0.1f) {
- avatar.CrossFade ("run");
- } else {
- avatar.CrossFade ("idle");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment