大体思路,同一个状态下定义多个枚举,可以通过该方法实现奔跑+急停。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player_MoveState : PlayerStateBase { //声明过渡 private float walk2RunTransition;
//枚举一下 private enum MoveChildState { Move, Stop }
private MoveChildState moveState; private MoveChildState MoveState { get => moveState; set { moveState = value; switch (moveState) { case MoveChildState.Move: //注册根运动 player_controller.Model.SetRootMotionAction(OnRootMotion); player_controller.PlayAnimation("Move"); break; case MoveChildState.Stop: player_controller.ClearCurrentSpeed(); player_controller.PlayAnimation("RunStop"); player_controller.Model.ClearRootMotionAction(); break; } } } public override void OnEnter() { base.OnEnter(); Debug.Log("状态转换成Move"); MoveState = MoveChildState.Move;
}
public void OnMoveUpdate() { //记录速度值 player_controller.currentSpeed = player_controller.Model.Animator.deltaPosition; //重力 player_controller.CharacterController.Move(new Vector3(0, player_controller.gravity * -1 * Time.deltaTime, 0));
//切换待机状态 if (GameInputManager.MainInstance.move == Vector2.zero) { //如果当前速度大于某个值 if(walk2RunTransition >= 0.7) { //切换为静止状态 MoveState = MoveChildState.Stop; } else { player_controller.ClearCurrentSpeed(); player_controller.ChangeState(PlayerState.Idle); return; } } else { CharacterRotate(); //如果按了加速键 if (GameInputManager.MainInstance.Speed) { walk2RunTransition = Mathf.Clamp(walk2RunTransition + player_controller.walk2runSpeed * Time.deltaTime, 0, 1); } else//跑到走 { walk2RunTransition = Mathf.Clamp(walk2RunTransition - player_controller.walk2runSpeed * Time.deltaTime, 0, 1); } player_controller.Model.Animator.SetFloat("Move", walk2RunTransition); //模型速度调整 player_controller.Model.Animator.speed = Mathf.Lerp(player_controller.walkSpeed, player_controller.runSpeed, walk2RunTransition); }
//TODO:切换翻滚状态
//TODO:切换攻击状态 }
private void OnStopUpdate() { //书写切换逻辑 if(CheckAnimatorName("RunStop",out float time)) { if(time >= 1f) { player_controller.ChangeState(PlayerState.Idle); } }
} public override void OnUpdate() { base.OnUpdate(); //进入跳跃逻辑 if (GameInputManager.MainInstance.Jump) { player_controller.ChangeState(PlayerState.Jump); return; } //下落逻辑 if (!IsReallyGround()) { player_controller.ChangeState(PlayerState.AirDown); }
//移动逻辑 switch (moveState) { case MoveChildState.Move: OnMoveUpdate(); break; case MoveChildState.Stop: OnStopUpdate(); break; }
//翻滚逻辑 if (GameInputManager.MainInstance.Roll) { player_controller.ChangeState(PlayerState.Roll); }
}
public override void OnExit() { walk2RunTransition = 0; player_controller.Model.ClearRootMotionAction(); player_controller.Model.Animator.speed = 1; }
//根运动逻辑 private void OnRootMotion(Vector3 deltaPosition, Quaternion deltaRotation) { deltaPosition.y = player_controller.gravity * -1 * Time.deltaTime; player_controller.CharacterController.Move(deltaPosition); }
//旋转逻辑 private void CharacterRotate() { Vector3 input = new Vector3(GameInputManager.MainInstance.move.x, 0, GameInputManager.MainInstance.move.y); Vector3 targetDir = Quaternion.Euler(0, Camera.main.transform.rotation.eulerAngles.y, 0) * input; player_controller.Model.transform.rotation = Quaternion.Slerp(player_controller.Model.transform.rotation, Quaternion.LookRotation(targetDir), Time.deltaTime * player_controller.rotateSpeed); } //运动数据清零逻辑 }
|