Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class AgentControl : MonoBehaviour {
- public Transform target;
- NavMeshAgent agent;
- Character character;
- // Use this for initialization
- void Start () {
- agent = GetComponent<NavMeshAgent>();
- character = GetComponent<Character>();
- StartCoroutine( LookForTarget() );
- }
- IEnumerator LookForTarget()
- {
- while(true)
- {
- Vector3 direction = (target.position+Vector3.up) - transform.position;
- Ray ray = new Ray( transform.position, direction );
- RaycastHit hit;
- Debug.DrawRay( ray.origin, ray.direction*direction.magnitude );
- if ( Physics.Raycast( ray, out hit ) )
- {
- if (hit.collider.tag == "Player")
- {
- agent.SetDestination( target.position );
- }
- Debug.Log (hit.collider.name);
- }
- yield return new WaitForSeconds(Random.value+0.5f);
- }
- }
- // Update is called once per frame
- void FixedUpdate () {
- int mask = 1 << NavMesh.GetNavMeshLayerFromName("No Running");
- NavMeshHit navHit;
- agent.SamplePathPosition (-1, 0, out navHit);
- bool walk = ((navHit.mask & mask) != 0);
- character.Move ( agent.desiredVelocity, walk, target.position );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment