forked from Unity-Technologies/NavMeshComponents
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClickToMove.cs
More file actions
27 lines (24 loc) · 708 Bytes
/
Copy pathClickToMove.cs
File metadata and controls
27 lines (24 loc) · 708 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using UnityEngine;
using UnityEngine.AI;
/// <summary>
/// Use physics raycast hit from mouse click to set agent destination
/// </summary>
[RequireComponent(typeof(NavMeshAgent))]
public class ClickToMove : MonoBehaviour
{
NavMeshAgent m_Agent;
RaycastHit m_HitInfo = new RaycastHit();
void Start()
{
m_Agent = GetComponent<NavMeshAgent>();
}
void Update()
{
if (Input.GetMouseButtonDown(0) && !Input.GetKey(KeyCode.LeftShift))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray.origin, ray.direction, out m_HitInfo))
m_Agent.destination = m_HitInfo.point;
}
}
}