Created
March 29, 2018 17:13
-
-
Save Templar2020/8e4f5296de96d8ccf03263bf1a9f277f to your computer and use it in GitHub Desktop.
Unity Nav Mesh Wander Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class Wander : MonoBehaviour { | |
public float wanderRadius; | |
public float wanderTimer; | |
private Transform target; | |
private NavMeshAgent agent; | |
private float timer; | |
// Use this for initialization | |
void OnEnable () { | |
agent = GetComponent<NavMeshAgent> (); | |
timer = wanderTimer; | |
} | |
// Update is called once per frame | |
void Update () { | |
timer += Time.deltaTime; | |
if (timer >= wanderTimer) { | |
Vector3 newPos = RandomNavSphere(transform.position, wanderRadius, -1); | |
agent.SetDestination(newPos); | |
timer = 0; | |
} | |
} | |
public static Vector3 RandomNavSphere(Vector3 origin, float dist, int layermask) { | |
Vector3 randDirection = Random.insideUnitSphere * dist; | |
randDirection += origin; | |
NavMeshHit navHit; | |
NavMesh.SamplePosition (randDirection, out navHit, dist, layermask); | |
return navHit.position; | |
} | |
} |
I got a CS0246 error can someone help me its on line 10 with NavMeshAgent it says there is no namespace
Add using UnityEngine.AI;
to the top of the file
Thanks for the comments and sorry for the error. These files are like a sketch/notepads for ideas.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
no need for - private Transform target;