Unity DOTs CharacterController简介

代码地址

https://github.com/Unity-Technologies/EntityComponentSystemSamples/tree/master/UnityPhysicsSamples/Assets/Demos/6.%20Use%20Cases/CharacterController

系统构成

输入系统

需要在ProjectSettings.asset中加入UNITY_INPUT_SYSTEM_EXISTS宏定义

  scriptingDefineSymbols:

    1: UNITY_POST_PROCESSING_STACK_V2;UNITY_INPUT_SYSTEM_EXISTS

输入的配置在Common/InputActions

DemoInputGatheringSystem 负责收入的接收,并写入CharacterControllerInput

角色控制系统

角色控制主要围绕CharacterControllerInternalData组件进行计算,Update收集并组成CharacterControllerJob,由这个Job完成运算工作

角色移动控制

  1. 落地检查CheckSupport:
    1. 碰撞检查,通过向下偏移ContactTolerance的射线检查是否有碰撞
    2. 如果无碰撞,设置状态为在空中,返回
    3. 根据碰撞点创建接触面信息列表constraints
    4. 由SimplexSolver.Solve求出支撑面,和速度
    5. 如果速度和初始速度没明显变化,一般为支撑面的坡度太大(近乎垂直,见代码Solve1D,速度会减去支撑面法向乘以速度),状态设置为在空中
    6. 如果速度很小,则状态设置为被支撑
    7. 否则跟最大滑坡角度比较,决定是被支撑还是在滑坡
  2.  HandleUserInput输入处理
    1. 根据输入确定移动方向和是否跳跃
    2. 旋转角色,见"角色旋转控制",同时要维持之前的速度方向(Velocity.Angular = -userRotationSpeed * up)
    3. 求出linearVelocity
  3.  如果不是在空中,用 CalculateMovement, 根据角色方向和所在平面重新计算速度
  4.  CollideAndIntegrate 碰撞处理并算出最终位置

角色旋转控制

Mouse Delta 被归一化了

Delta Mouse说明:https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Mouse.html

应该是屏幕像素差

计算如下:

bool haveInput = (math.abs(horizontal) > float.Epsilon);
if (haveInput)
{
    var userRotationSpeed = horizontal * ccComponentData.RotationSpeed;
    ccInternalData.Velocity.Angular = -userRotationSpeed * up;
    ccInternalData.CurrentRotationAngle += userRotationSpeed * DeltaTime;
}

手的旋转控制

原有角度乘以绕x轴旋转的矩阵

float a = -input.Looking.y;

gunRotation.Value = math.mul(gunRotation.Value, quaternion.Euler(math.radians(a), 0, 0));

Unity.Mathematics缺少将四元素转欧拉的函数,这个在Unity.Physics.Math中有说明

// Note: taken from Unity.Animation/Core/MathExtensions.cs, which will be moved to Unity.Mathematics at some point
//       after that, this should be removed and the Mathematics version should be used
#region toEuler

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值