Skip to content

Instantly share code, notes, and snippets.

@Templar2020
Created March 29, 2018 17:13
Show Gist options
  • Save Templar2020/8e4f5296de96d8ccf03263bf1a9f277f to your computer and use it in GitHub Desktop.
Save Templar2020/8e4f5296de96d8ccf03263bf1a9f277f to your computer and use it in GitHub Desktop.
Unity Nav Mesh Wander Script
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;
}
}
@LynJuice
Copy link

no need for - private Transform target;

@xmdcnoii
Copy link

I got a CS0246 error can someone help me its on line 10 with NavMeshAgent it says there is no namespace

@carlosdperezruiz
Copy link

Add using UnityEngine.AI; to the top of the file

@hamiltopia
Copy link

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