此为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 updatevoid Start(){animator = GetComponent<Animator>();}// Update is called once per framevoid 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){//设置头部IKanimator.SetLookAtWeight(1);animator.SetLookAtPosition(target.position);//设置右手IKanimator.SetIKPositionWeight(AvatarIKGoal.RightHand,1);//旋转权重animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);//设置右手IKanimator.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 updatevoid Start(){agent = GetComponent<NavMeshAgent>();}// Update is called once per framevoid 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的区域遮罩可以和区域成本结合起来,让玩家选择性的无法通过某个区域