栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

Unity使用单例模式已经NavMeshAgent实现鼠标点击移动导航

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity使用单例模式已经NavMeshAgent实现鼠标点击移动导航


为要添加移动的角色添加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();//获取控件
    }
    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 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 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设置到那个点

        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/906053.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号