【学习日志】2022.10.08 Unity人物运动(移动+转身)、Character Controller、射线检测、动画融合
Angelyatou/Endless_Unity_Projects: Unity Projects of Endlessdaydram (github.com)https://github.com/Angelyatou/Endless_Unity_Projects
最简单的人物运动(移动+转身)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Cha1 : MonoBehaviour
{public float speed = 3;Vector3 move;void Update(){//获取输入float h = Input.GetAxis("Horizontal");float v = Input.GetAxis("Vertical");Move(h, 0, v);}void Move(float x,float y,float z){//世界坐标move = new Vector3(x, y, z);//要看向的位置Vector3 to = transform.position + move;transform.LookAt(to);transform.position += move * speed * Time.deltaTime;}
}
FBX文件(包含模型、骨骼、动画、材质)
动画状态机
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Cha1 : MonoBehaviour
{Animator animator;public float speed = 3;Vector3 move;private void Start(){animator = GetComponent<Animator>();}void Update(){//获取输入float h = Input.GetAxis("Horizontal");float v = Input.GetAxis("Vertical");Move(h, 0, v);//更新动画UpdateAnim();}void Move(float x,float y,float z){//世界坐标move = new Vector3(x, y, z);//要看向的位置Vector3 to = transform.position + move;transform.LookAt(to);transform.position += move * speed * Time.deltaTime;}void UpdateAnim(){float forward = move.magnitude;animator.SetFloat("Forward", forward);}
}
把FBX里的动画文件拖到Animator窗口,设置动画状态机
添加刚体、碰撞体并冻结刚体旋转
然后发现在撞东西的时候会抖,像这样
于是我们修改一下代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Cha2 : MonoBehaviour
{Animator animator;Rigidbody rigid;public float speed = 3;Vector3 move;private void Start(){animator = GetComponent<Animator>();rigid = GetComponent<Rigidbody>();}void Update(){//获取输入float h = Input.GetAxis("Horizontal");float v = Input.GetAxis("Vertical");Move(h, 0, v);//更新动画UpdateAnim();}void Move(float x, float y, float z){//世界坐标move = new Vector3(x, y, z);}//刚体移动,要在FixedUpdate里写public void FixedUpdate(){//根据move,直接改变刚体速度Vector3 v = move * speed;v.y = rigid.velocity.y;rigid.velocity = new Vector3(move.x,rigid.velocity.y,move.z)* speed;//让刚体朝向目标Quaternion q = Quaternion.LookRotation(move); //向量 转成 朝向rigid.MoveRotation(q);}void UpdateAnim(){float forward = move.magnitude;animator.SetFloat("Forward", forward);}
}
就变成这样了
如果想让他碰到东西停下动作,也可以把UpdateAnim改成这样
void UpdateAnim(){//float forward = move.magnitude;//animator.SetFloat("Forward", forward);//基于刚体速度播放动画animator.SetFloat("Forward", rigid.velocity.magnitude / speed);}
就变成这样
使用Character Controller控制角色运动(移动+转身)
角色控制器
Step Offset:上楼梯的台阶高度最大值
Skin Width:皮肤(柔软物质厚度)(避免卡死)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Cha3 : MonoBehaviour
{Animator animator;CharacterController cc;public float speed = 3;Vector3 move;private void Start(){animator = GetComponent<Animator>();cc = GetComponent<CharacterController>();}void Update(){//获取输入float h = Input.GetAxis("Horizontal");float v = Input.GetAxis("Vertical");Move(h, 0, v);//更新动画UpdateAnim();}void Move(float x, float y, float z){move = new Vector3(x, 0, z);//这一帧移动的向量,很小Vector3 m = move * speed * Time.deltaTime;//朝向移动方向transform.LookAt(transform.position + m);//通过cc去移动cc.Move(m);}void UpdateAnim(){//基于刚体速度播放动画animator.SetFloat("Forward",cc.velocity.magnitude / speed);}
}
运行unity发现能爬楼梯爬坡但不下落
使用射线检测改进(使人物能下落)
Test Raycast:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Cha3Fall : MonoBehaviour
{Animator animator;CharacterController cc;public float speed = 3;Vector3 move;bool isGround = true;private void Start(){animator = GetComponent<Animator>();cc = GetComponent<CharacterController>();}void Update(){//获取输入float h = Input.GetAxis("Horizontal");float v = Input.GetAxis("Vertical");Move(h, 0, v);//更新动画UpdateAnim();}void Move(float x, float y, float z){move = new Vector3(x, 0, z);//这一帧移动的向量,很小Vector3 m = move * speed * Time.deltaTime;//朝向移动方向transform.LookAt(transform.position + m);//通过cc去移动cc.Move(m);}private void FixedUpdate(){Ray ray = new Ray(transform.position + new Vector3(0, 0.2f, 0), Vector3.down);RaycastHit hit;Color c = Color.red;isGround = false;if(Physics.Raycast(ray,out hit, 0.35f)){c = Color.green;isGround = true;}//调式Debug.DrawLine(transform.position + new Vector3(0, 0.2f, 0),transform.position - new Vector3(0,0.15f,0),c);}void UpdateAnim(){//基于刚体速度播放动画animator.SetFloat("Forward", cc.velocity.magnitude / speed);}
}
下落:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Cha3Fall : MonoBehaviour
{Animator animator;CharacterController cc;public float speed = 3;Vector3 move;bool isGround = true;private void Start(){animator = GetComponent<Animator>();cc = GetComponent<CharacterController>();}void Update(){//获取输入float h = Input.GetAxis("Horizontal");float v = Input.GetAxis("Vertical");Move(h, 0, v);//更新动画UpdateAnim();}//纵向速度float vy = 0;void Move(float x, float y, float z){move = new Vector3(x, 0, z);//这一帧移动的向量,很小Vector3 m = move * speed * Time.deltaTime;if (isGround){vy = 0;}else{//物理公式:v = v0 + gt (v0=0)vy += Physics.gravity.y * Time.deltaTime;}//Δy = v * Δtm.y = vy * Time.deltaTime;//朝向移动方向transform.LookAt(transform.position + m);//通过cc去移动cc.Move(m);}private void FixedUpdate(){Ray ray = new Ray(transform.position + new Vector3(0, 0.2f, 0), Vector3.down);RaycastHit hit;Color c = Color.red;isGround = false;if(Physics.Raycast(ray,out hit, 0.35f)){c = Color.green;isGround = true;}//调式Debug.DrawLine(transform.position + new Vector3(0, 0.2f, 0),transform.position - new Vector3(0,0.15f,0),c);}void UpdateAnim(){//基于刚体速度播放动画animator.SetFloat("Forward", cc.velocity.magnitude / speed);}
}
动画融合
加个Test Speed进行调试
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Cha3Fall : MonoBehaviour
{Animator animator;CharacterController cc;public float speed = 3;[Range(0.0f, 1.0f)]public float testSpeed = 1;Vector3 move;bool isGround = true;private void Start(){animator = GetComponent<Animator>();cc = GetComponent<CharacterController>();}void Update(){//获取输入float h = Input.GetAxis("Horizontal");float v = Input.GetAxis("Vertical");Move(h, 0, v);//更新动画UpdateAnim();}//纵向速度float vy = 0;void Move(float x, float y, float z){move = new Vector3(x, 0, z);//这一帧移动的向量,很小Vector3 m = move * speed * Time.deltaTime;if (isGround){vy = 0;}else{//物理公式:v = v0 + gt (v0=0)vy += Physics.gravity.y * Time.deltaTime;}//Δy = v * Δtm.y = vy * Time.deltaTime;//朝向移动方向transform.LookAt(transform.position + move);//通过cc去移动cc.Move(m);}private void FixedUpdate(){Ray ray = new Ray(transform.position + new Vector3(0, 0.2f, 0), Vector3.down);RaycastHit hit;Color c = Color.red;isGround = false;if(Physics.Raycast(ray,out hit, 0.35f)){c = Color.green;isGround = true;}//调式Debug.DrawLine(transform.position + new Vector3(0, 0.2f, 0),transform.position - new Vector3(0,0.15f,0),c);}void UpdateAnim(){//基于刚体速度播放动画animator.SetFloat("Forward", cc.velocity.magnitude / speed * testSpeed);}
}
【学习日志】2022.10.08 Unity人物运动(移动+转身)、Character Controller、射线检测、动画融合相关推荐
- 【开发日志】2022.10.06 Unity自制小游戏《飞龙射击》全过程详解
Angelyatou/Endless_Unity_Projects: Unity Projects of Endlessdaydram (github.com)https://github.com/A ...
- 恶意软件免杀与技术(2022.10.08)
APT41免杀技术 在感染过程中两次执行相同的 CHM 文件.第一次执行表现出良性活动,而第二次执行隐秘地执行恶意行为. 最初执行体是名为 pss10r.chm (SHA256: 3d279aa8f5 ...
- 学习日志(10.21)
今天主要讲了脚本的使用基础 1脚本的循环语句 循环创建用户 #!/bin/sh for((i=0;i<3;i++)) douseradd $icd /home/$iecho "This ...
- Unity 解决子弹穿模问题,2D射线检测
子弹射速过快导致的穿模问题(2D) public void BulletMovement() { //记录位置 posRecord = transform.po ...
- 2022年全球市场PCB X射线检测系统总体规模、主要生产商、主要地区、产品和应用细分研究报告
本文研究全球市场.主要地区和主要国家PCB X射线检测系统的销量.销售收入等,同时也重点分析全球范围内主要厂商(品牌)竞争态势,PCB X射线检测系统销量.价格.收入和市场份额等.针对过去五年(201 ...
- 信奥中的数学学习资料汇总(2022.10.31)
信奥中的数学之入门组(面向小学四年级至六年级以及初一学生) 信奥中的数学之入门组(面向小学四年级至六年级以及初一学生)_dllglvzhenfeng的博客-CSDN博客 信奥中的数学学习:小学.初高中 ...
- Python学习日志08 - 字典
Python学习日志 RBHGO的主页欢迎关注 温馨提示:创作不易,如有转载,注明出处,感谢配合~ 目录 文章目录 Python学习日志 目录 Python学习日志08课 - 字典 前言 进入正题 创 ...
- 少儿C++编程学习路线推荐(2022.10.31)
一.C++学习路线推荐 1.小学一二年级C++启蒙 (也可以同时加强数学,比如学一点儿小学奥数或提前学小学三四五六年级的数学知识) (1).<我的第一本算法书> 少儿C++编程如何入门 少 ...
- 适合人工智能的编程语言有哪些 人工智能学习路线(2022.10.16)
人工智能ai用什么编程语言_用于AI开发的6种最佳编程语言 人工智能ai用什么编程语言_用于AI开发的6种最佳编程语言_cxq8989的博客-CSDN博客 最适合人工智能开发的六种编程语言 最适合人工 ...
- Unity小狐狸学习日志、关于角色创立,移动控制以及跳跃动画等方面
============================================================== GMAE:2d 导入数据包:创建项目后在unity商店导入数据包. 数据载 ...
最新文章
- 用jk触发器构成二分频电路_实例分析,轻松掌握声控照明电路
- Python打包程序
- xtrabackup 安装、备份和恢复
- 让Windows 2000/XP系统自动登陆
- 重磅推荐:2020年人工智能最精彩的25篇论文(附下载)
- window中osmnx包的详细安装过程
- 战地2服务器怎么虚拟人数,战地2怎么修改作战人数?
- 程序员造轮子的正确姿势
- 华为云张昆:支持全场景全业务,GaussDB加速企业数字化转型
- Spring容器创建流程(8)初始化bean
- python中的urllib模块中的方法
- 物联网智能路灯运维解决方案
- 离散数学第二版屈婉玲教材pdf_离散数学 第二版 [屈婉玲,耿素云,张立昂 编著] 2015年版...
- vue引入阿里云图标
- 大班音乐机器人反思_大班音乐活动机器人
- 迅为4412开发平台Zigbee模块在物联网智能家居中的应用
- 图纸打印什么时候用蓝图_为什么工程图纸都是蓝色的?是叫“蓝图”吗?
- Matlab绘制经纬度地图并添加坐标点
- Step1我学习ros2的一些经历(从ubuntu安装到ros2中的位姿转换)
- 安防视频监控系统方案 现代机场安防视频监控系统