Advertisement
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;
- float originalSpeed;
- // Use this for initialization
- void Start () {
- agent = GetComponent<NavMeshAgent>();
- originalSpeed = agent.speed;
- 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 Update () {
- int mask = 1 << NavMesh.GetNavMeshLayerFromName("No Running");
- NavMeshHit navHit;
- agent.SamplePathPosition (-1, 0, out navHit);
- if ( (navHit.mask & mask) != 0)
- {
- agent.speed = originalSpeed * 0.3f;
- } else {
- agent.speed = originalSpeed;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement