为要添加移动的角色添加NavMeshAgent 然后绑定脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class playerManager : MonoBehaviour
{
private NavMeshAgent agent;
void Awake()
{
agent = GetComponent<NavMeshAgent>();
}
void Start()
{
CurseMove.Instance.OnMouseClicked += MoveToTarget;
}
public void MoveToTarget(Vector3 target)
{
agent.destination = target;
}
}
创建一个空物体搭载单例脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using System;
public class CurseMove : MonoBehaviour
{
public static CurseMove Instance;
RaycastHit hitInfo;
public event Action<Vector3> OnMouseClicked;
void Awake()
{
if (Instance != null)
{
Destroy(gameObject);
}
Instance = this;
}
void Update()
{
ChangeCursorTexture();
MouseControl();
}
void ChangeCursorTexture()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hitInfo))
{
}
}
void MouseControl()
{
if (Input.GetMouseButtonDown(0) && hitInfo.collider != null)
{
if (hitInfo.collider.gameObject.CompareTag("Ground"))
{
OnMouseClicked?.Invoke(hitInfo.point);
}
}
}
}
设置地图tag并且设置为NavMesh的static静态变量点击bake进行烘焙选择可行区域还是不可行
public Texture2D point, attack, doorwway, target, arrow;
witch (hitInfo.collider.gameObject.tag) {
case "Ground":
Cursor.SetCursor(target, new Vector2(16, 16), CursorMode.Auto);
break;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using System;
public class CurseMove : MonoBehaviour
{
public static CurseMove Instance;
RaycastHit hitInfo;
public event Action<Vector3> OnMouseClicked;
public Texture2D point, attack, doorwway, target, arrow;
void Awake()
{
if (Instance != null)
{
Destroy(gameObject);
}
Instance = this;
}
void Update()
{
ChangeCursorTexture();
MouseControl();
}
void ChangeCursorTexture()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hitInfo))
{
switch (hitInfo.collider.gameObject.tag) {
case "Ground":
Cursor.SetCursor(target, new Vector2(16, 16), CursorMode.Auto);
break;
}
}
}
void MouseControl()
{
if (Input.GetMouseButtonDown(0) && hitInfo.collider != null)
{
if (hitInfo.collider.gameObject.CompareTag("Ground"))
{
OnMouseClicked?.Invoke(hitInfo.point);
}
}
}
}
|