滑铲到悬崖边下落时无法取消动画
由于是使用的协程方式实现,所以当滑铲到悬崖边的时候,不能使用yield break
,因为该指令会直接退出当前的协程方法,无法执行到isSlide = false
指令
private IEnumerator TriggerSlide(Vector3 target)
{
do
{
yield return null;
if(!physicsCheck.isGround)
{
break;
}
if (physicsCheck.touchLeftWall || physicsCheck.touchRightWall)
{
isSlide = false;
break;
}
rb.MovePosition(new Vector2(transform.position.x + transform.localScale.x * slideSpeed, transform.position.y));
} while (Math.Abs(target.x - transform.position.x) > 0.1f);
isSlide = false;
}
滑铲时无敌(不受到敌人伤害)
通过修改人物的Layer实现,滑铲时更改到和敌人相同的Layer
if (!isSlide && physicsCheck.isGround)
{
isSlide = true;
var targetPosition = new Vector3(transform.position.x + slideDistance * transform.localScale.x, transform.position.y);
gameObject.layer = LayerMask.NameToLayer("Enemy");
StartCoroutine(TriggerSlide(targetPosition));
}
滑铲完毕后,改回玩家的Layer
gameObject.layer = LayerMask.NameToLayer("Player");
靠墙时无法滑铲
之前代码中,触及墙壁不给滑铲,目的是滑铲过程中撞墙取消滑铲。所以,添加一个条件,即面朝墙壁方向时
// 滑动过程中撞墙
if (physicsCheck.touchLeftWall && transform.localScale.x < 0f || physicsCheck.touchRightWall && transform.localScale.x > 0f)
{
isSlide = false;
break;
}
跳跃打断滑铲
// 打断滑铲协程
StopAllCoroutines();
isSlide = false;
标签:false,isSlide,localScale,transform,2D,Unity,引擎,position,physicsCheck
From: https://www.cnblogs.com/xinlindeyu/p/17999710