为要添加移动的角色添加NavMeshAgent
然后绑定脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class playerManager : MonoBehaviour
{
// Start is called before the first frame update
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;//创建一个Action事件OnMouseClicked参数是Vector3 所以订阅这个事件的函数参数类型也是Vector3
//用于从光线投射中获取信息的结构。
void Awake()//创建一个单例 始终使得只有这一个类的静态方法
//如果这个Instance不为空就销毁gameObject对象 否则就创建Instance
{
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 Invoke内容为Vector3这个值
OnMouseClicked?.Invoke(hitInfo.point);
}//每次点击鼠标点击到地面的时候可以把Agent 的destination设置到那个点
}
}
}
设置地图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);
//使用鼠标内置方法SetCursor 参数是一个Texture2D图片
//一个偏移量比如这个图片大小是32*32那么他的中心在左上角偏移量为
//16*16就是32*32的中心 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;//创建一个Action事件OnMouseClicked参数是Vector3 所以订阅这个事件的函数参数类型也是Vector3
//用于从光线投射中获取信息的结构。
public Texture2D point, attack, doorwway, target, arrow;
void Awake()//创建一个单例 始终使得只有这一个类的静态方法
//如果这个Instance不为空就销毁gameObject对象 否则就创建Instance
{
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);//使用鼠标内置方法SetCursor 参数是一个Texture2D图片 一个偏移量比如这个图片大小是32*32那么他的中心在左上角偏移量为
//16*16就是32*32的中心 CursorMode默认设置为Auto
break;
}
}
}
void MouseControl()
{
if (Input.GetMouseButtonDown(0) && hitInfo.collider != null) //点击鼠标左键并且点击的碰撞体不为空
{
if (hitInfo.collider.gameObject.CompareTag("Ground"))
{//如果点击的是地面的话
//如果这个事件即OnMouseClicked不为空的话执行Invoke Invoke内容为Vector3这个值
OnMouseClicked?.Invoke(hitInfo.point);
}//每次点击鼠标点击到地面的时候可以把Agent 的destination设置到那个点
}
}
}