Skip to content

Instantly share code, notes, and snippets.

@tarukosu
Created April 23, 2017 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarukosu/d634a5580503899c48e7893ec546f247 to your computer and use it in GitHub Desktop.
Save tarukosu/d634a5580503899c48e7893ec546f247 to your computer and use it in GitHub Desktop.
RandomWalk.cs
using UnityEngine;
using UnityEngine.AI;
// Walk to a random position and repeat
[RequireComponent(typeof(NavMeshAgent))]
public class RandomWalk : MonoBehaviour
{
public float m_Range = 3.0f;
NavMeshAgent m_agent;
public bool drawPath = false;
LineRenderer lineRenderer;
void Start()
{
m_agent = GetComponent<NavMeshAgent>();
if(drawPath)
{
lineRenderer = gameObject.AddComponent<LineRenderer>();
}
}
void Update()
{
if (m_agent.pathPending || m_agent.remainingDistance > 0.1f)
{
return;
}
var nextPoint = m_Range * Random.insideUnitCircle;
m_agent.destination = m_agent.transform.position + new Vector3(nextPoint.x, 0, nextPoint.y);
if (drawPath)
{
var positions = m_agent.path.corners;
lineRenderer.widthMultiplier = 0.1f;
lineRenderer.positionCount = positions.Length;
for (int i = 0; i < positions.Length; i++)
{
lineRenderer.SetPosition(i, positions[i]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment