看到相机建在playermovement下,蚌埠住了
1.先设置input system,设置成键盘按键
2.为角色设置刚体,碰撞体
3.
任务四
input System 可以实现多设备玩游戏
PlayerCon
PlayerMovementAction
如果你发现你代码里的b_Input=inpu
如果无法长按奔跑和短按翻滚,可能是InputHandler脚本HandleRollInput方法中:
b_Input = inputActions.PlayerActions.Roll.phase == UnityEngine.InputSystem.InputActionPhase.Started;
该语句由于某些原因使得b_Input没有在按下shift时正常设置值为true导致的。经测试在我的系统环境下没有started的phase,导致这个问题产生。解决办法是将Started替换为Performed:
b_Input = inputActions.PlayerActions.Roll.phase == UnityEngine.InputSystem.InputActionPhase.Performed;
之前因为这个问题还试了另一种用委托的写法,但是为了不和课程后面的代码越跑越远还是改掉了,这里发一下作为参考(留念)。
在InputHandler脚本中:
0.不使用HandleRollInput方法,全部注释掉
1.声明一个用于开关计时器的布尔变量isTimer和两个委托方法SprintActions和RollActions
public bool isTimer;
public void SprintActions(InputAction.CallbackContext context)//参数是为了与事件的委托类型相匹配
{
isTimer = true;//开启计时器
}
public void RollActions(InputAction.CallbackContext context)
{
isTimer = false;//关闭计时器
sprintFlag = false;//停止奔跑
if (rollInputTimer > 0 && rollInputTimer < 0.5)
rollFlag = true;//判断后进行翻滚
rollInputTimer = 0;//重置计时器
}
2.在OnEnable方法中添加两个新的委托,如下添加注释部分:
public void OnEnable()
{
if (inputActions == null)
{
inputActions = new PlayerControls();
inputActions.PlayerMovement.MovementAction.performed += outputActions => movementInput = outputActions.ReadValue<Vector2>();
inputActions.PlayerMovement.Camera.performed += cameraActions => cameraInput = cameraActions.ReadValue<Vector2>();
inputActions.PlayerActions.Roll.performed += SprintActions;//在按下shift后的执行状态触发事件performed时,调用一次委托方法SprintActions
inputActions.PlayerActions.Roll.canceled += RollActions;//在按下shift后的取消(结束)状态触发事件canceled时,调用一次委托方法RollActions
}
inputActions.Enable();
}
3.在Update方法中添加如下代码:
void Update()
{
if (isTimer)//如果计时器开启
{
rollInputTimer += Time.deltaTime;//计时器值持续增加
if (rollInputTimer > 0 && rollInputTimer > 0.5)//如果shift为长按
sprintFlag = true;//开始奔跑
}
}
经测试也可以达到长按奔跑短按翻滚的效果。但是最后发现既然这里的performed事件没有问题的话,不如直接把原来代码的Started改成Performed。
个人学习笔记,如果有不对的地方请多多指教
(:3[▓▓]
配置动画animator -> blend tree ->2d type
AnimatorHandler.cs
initialize()
UpdateAnimatorValues():->归一化XY两轴值,并更新: -> SetFloat
---------------------------------playerLocoMotion.cs
- 将输入行为和动画控制进行分离;
- InputHandler:
绑定InputSystem 的 performed Action
捕获输入参数: horizontal Vertical moveAmount mouseX mouseY
实现 TickInput方法,
- PlayerLocoMotion.cs:
实现移动:
inputHandler.tickInput()
velocity ->
rb.velocity
实现旋转:
var dir = Quaternion.LookRotation(targetV);
rb.rotation = Quaternion.Slerp(rb.rotation, dir, rotationSpeed * delta);