当前位置: 首页 > article >正文

Unity学习part3

此为b站视频【【Unity教程】零基础带你从小白到超神】 https://www.bilibili.com/video/BV1gQ4y1e7SS/?p=55&share_source=copy_web&vd_source=6e7a3cbb802eb986578ad26fae1eeaab的笔记

1、反向动力学

打开ik处理

public class PlayerMoveController : MonoBehaviour
{
    public Transform target;
    private Animator animator;
    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        //水平轴
        float horizontal = Input.GetAxis("Horizontal");
        //垂直轴
        float vertical = Input.GetAxis("Vertical");
        //向量
        Vector3 dir = new Vector3(horizontal,0, vertical);

        //当用户按下方向键
        if (dir != Vector3.zero)
        {
            //面向向量
            transform.rotation = Quaternion.LookRotation(dir);
            //播放跑步动画
            animator.SetBool("isRun", true);
            //朝向前方移动
            transform.Translate(Vector3.forward * 2 * Time.deltaTime);
        }
        else {
            //播放站立动画
            animator.SetBool("isRun", false);
        }

        //随时获取test参数并打印出来
        //Debug.Log(animator.GetFloat("Test"));
    }

    void leftfoot()
    {
        //可以在这边播放音效或者添加特效
        Debug.Log("左脚");
    }

    void rightfoot()
    {
        Debug.Log("右脚");
    }

    //Ik写到这个方法
    private void OnAnimatorIK(int layerIndex)
    {
        //设置头部IK
        animator.SetLookAtWeight(1);
        animator.SetLookAtPosition(target.position);
        //设置右手IK
        animator.SetIKPositionWeight(AvatarIKGoal.RightHand,1);
        //旋转权重
        animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
        //设置右手IK
        animator.SetIKPosition(AvatarIKGoal.RightHand, target.position);
        animator.SetIKRotation(AvatarIKGoal.RightHand, target.rotation);
    }
}

 2、unity导航组件

用于寻路。unity 2022需要窗口-包管理器-Unity注册表-AI Navigation-安装。安装后,菜单栏选择Window-AI-Navigation(Obsolete)-Object标签下,有Navigation Static勾选,下面就和up视频中一样了(弹幕),添加组件之后点击bake获取烘焙结果

玩家需要添加nav mesh agent组件 ,代理类型可以添加不同体型半径高度的体积

public class AgentMove : MonoBehaviour
{
    private NavMeshAgent agent;
    // Start is called before the first frame update
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
    }

    // Update is called once per frame
    void Update()
    {
        //如果按下鼠标
        if (Input.GetMouseButtonDown(0)) { 
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                //点击位置
                Vector3 point = hit.point;
                agent.SetDestination(point);
            }
        }
    }
}

动态障碍物,勾选切割之后会动态的重新计算导航地图。移动阈值是指移动多少时会重新开始计算导航地图。静止时间是指障碍物在移动时即使到达移动阈值任不会重新计算导航地图,而是要等待障碍物停止移动后多少秒才重新开始计算。取消仅在静止时切割的话,障碍物移动时导航地图实时计算,消耗高

 

相关导航因为版本更新需查询其他内容 

nav mesh agent的区域遮罩可以和区域成本结合起来,让玩家选择性的无法通过某个区域

 


http://www.kler.cn/a/550852.html

相关文章:

  • ubuntu24基于虚拟机无法从主机拖拽文件夹
  • 数据结构:栈(Stack)及其实现
  • 在 UniApp 项目中设置多语言
  • GO语言中的悲观锁与乐观锁
  • Python排序算法详解
  • 坐井说天阔---DeepSeek-R1
  • 设计模式2:单例模式
  • Docker 容器日志与监控
  • 【对比】Pandas 和 Polars 的区别
  • Unity截取RenderTexture某帧画面显示在Image上
  • Leetcode 算法题 88. 合并两个有序数组
  • 802.3 两种格式
  • Redis 10章——集群(cluster)
  • 服务器A到服务器B免密登录
  • 【拒绝算法PUA】LeetCode 1287. 有序数组中出现次数超过25%的元素
  • maven使用默认settings.xml配置时,Idea基于pom.xml更新依赖时报错,有些组件下载时连接超时
  • 解锁JavaScript新姿势:Set数据结构深度解析
  • Unity Shader示例 6: 卡渲基础 - 描边 + 着色
  • 【学术投稿-第四届智能电网和绿色能源国际学术会议(ICSGGE 2025)】CSS基本选择器详解:掌握基础,轻松布局网页
  • 深入剖析 Python 爬虫:淘宝商品详情数据抓取