summaryrefslogtreecommitdiff
path: root/Other/NavMeshTest/Assets/Scripts/NavigateToTarget.cs
blob: a7ed37414cca268e417718b32d72146f254d7fbf (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class NavigateToTarget : MonoBehaviour
{
    public Transform flag;

    public NavMeshAgent agent;

    private Transform m_Target;
    private Vector3 m_TargetPosition;

    private void Awake()
    {
        agent = GetComponent<NavMeshAgent>();

        agent.autoTraverseOffMeshLink = false;
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit info;
            if (Physics.Raycast(ray, out info, Mathf.Infinity, Physics.DefaultRaycastLayers))
            {
                if (info.collider.gameObject.layer != LayerMask.NameToLayer("Prop"))
                {
                    m_Target = info.collider.gameObject.transform;
                    m_TargetPosition = m_Target.InverseTransformPoint(info.point);
                }
            }
        }

        if(m_Target != null)
        {
            Vector3 pos = m_Target.TransformPoint(m_TargetPosition);

            agent.destination = pos;
            flag.position = pos;
        }

        if(agent.isOnOffMeshLink)
        {

        }

    }

}