一:人物翻滚
要点:Roll 键和Run 键绑定同时绑定在了Shift按键上,采取的做法时将Run的按键方式设置成Hold,将Roll的按键方式设置成Release Only。并且翻滚时不能为奔跑的状态。(长按Shift)
由于翻滚动画后半段带一个攻击效果,rollAnimationOver变量表示翻滚的完整动画是否已经结束。(可以转为用animator的标签进行判断)
//开始翻滚
private bool CanRoll()
{
return physicalCheck.IsOnGround && playerCurrentStats.CurEnergy >= rollCost && !playerAnimationInf.IsRoll && !playerAnimationInf.IsRun && playerAnimationInf.rollAnimationOver && !playerAnimationInf.IsFocusAttack && !playerAnimationInf.IsChangeWeapon;
}
private void StartRoll(InputAction.CallbackContext obj)
{
//如果人物在地面上并且人物的能量足够 才进行翻滚
if (CanRoll())
{
//消耗能量 变更为无敌状态
StopAllCoroutines();playerAnimationInf.StopAllCoroutines();
playerCurrentStats.ExpendEnergy(rollCost);
playerCurrentStats.InvincibleWhenRoll = true;
playerAnimationInf.IsRoll = true;
playerAnimationInf.rollAnimationOver = false;
//StartCoroutine(RollForward()); //转变为由动画控制移动
}
}
在结束跑步的函数时候要进行一个延迟调用(0s的Invoke,效果是跳过这一帧),确保松开Shift键后不会直接翻滚。(翻滚会被IsRun的状态限制住)
//结束跑步
private void CancelRun(InputAction.CallbackContext obj)
{
//延迟调用? (由于Roll和Run是同一个按键的不同交互导致的,确保松开Shift后不会翻滚)
Invoke(nameof(CancelRun), 0f);
}
翻滚的实际移动在动画中控制,和人物攻击时的移动修正一致。
翻滚会造成的额外效果(如无敌)额外进行设置即可。
二:人物跳跃
要点:实现一个连续的跳跃(一直按着空格时可以在落地后继续进行跳跃)
对于按键的绑定函数:其中jumpButton表示现在是否还在按着跳跃键
//开始跳跃
private void StartJump(InputAction.CallbackContext obj)
{
jumpButton = true;
}
//结束跳跃
private void CancelJump(InputAction.CallbackContext obj)
{
jumpButton = false;
}
可以指定要跳跃的高度JumpHeight,利用物理公式 v=根号下(2gh)实现
private void Jump()
{
anim.SetTrigger("Jump");
playerAnimationInf.landAnimationOver = false;
velocityByGravity.y = Mathf.Sqrt(-2 * gravity * jumpHeight);
}
另外要求人物不处在地面的时候要进行下降(即考虑重力效果)
private void Gravitation()
{
//考虑重力
if (physicalCheck.IsOnGround && velocityByGravity.y < 0) velocityByGravity.y = 0;
else velocityByGravity.y += gravity * Time.deltaTime;
characterController.Move(velocityByGravity * Time.deltaTime);
}
为了实现连续的跳跃效果,在Update中进行如下判断:
状态信息landAnimationOver表示落地的动画是否结束。(可以用动画标签)
if (jumpButton&&physicalCheck.IsOnGround&&!playerAnimationInf.IsRoll&&!playerAnimationInf.IsAttack&& playerAnimationInf.landAnimationOver) Jump();