Created
April 23, 2017 09:24
-
-
Save tarukosu/d634a5580503899c48e7893ec546f247 to your computer and use it in GitHub Desktop.
RandomWalk.cs
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 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