duck

Unity 3D Navesh Agent Control - step 1

Feb 27th, 2013
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.69 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class AgentControl : MonoBehaviour {
  5.    
  6.     public Transform target;
  7.     NavMeshAgent agent;
  8.     float originalSpeed;
  9.    
  10.     // Use this for initialization
  11.     void Start () {
  12.         agent = GetComponent<NavMeshAgent>();
  13.         originalSpeed = agent.speed;
  14.     }
  15.    
  16.     // Update is called once per frame
  17.     void Update () {
  18.        
  19.         int mask = 1 << NavMesh.GetNavMeshLayerFromName("No Running");
  20.        
  21.         NavMeshHit navHit;
  22.         agent.SamplePathPosition (-1, 0, out navHit);
  23.        
  24.        
  25.         if ( (navHit.mask & mask) != 0)
  26.         {
  27.             agent.speed = originalSpeed * 0.3f;
  28.            
  29.         } else {
  30.             agent.speed = originalSpeed;
  31.         }  
  32.        
  33.         agent.SetDestination( target.position );
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment