仅当特定攻击动画完成查询时才使对象实例化
Making object Instantiate only when the specific attack animation is complete query
我的玩家Robot
在我点击攻击按钮时正在攻击,如下所示。我正在使用 coroutine
等待一段时间,然后实例化激光形式 firepos
位置。
请看下面的内容gif
,以便更清楚地理解:
现在如果你看到上面的 gif
你可以看到如果我按住 攻击 按钮玩家正在做他的动画并且根据 waitForSeconds
coroutine
一秒后激光发射。到现在都很好!
但是当我在短时间内突然多次点击按钮时,动画 不播放(我能理解..这很明显!) 但就像上面的情况一样过了一段时间(由于 waitforseconds
)并且由于 我没有点击 相同数量的激光发射 我不想 !
我想要的是当我点击并按住攻击按钮时,动画应该完成,然后激光应该发射。
AND
如果我在短时间内多次点击它,激光(这是一个正在实例化的预制件)应该不会发射。
只有当动画完成时激光才会发射。
现在我想不通了!任何人都可以帮忙。
检查器中的按钮(攻击):
我在 OnpointerDown
和 up
中调用两个函数
功能代码:
public void LaserAttack(){
isAttacking = true;
StartCoroutine (AttackStillCoroutine ());
if (robotLeft == true&&isLaserLeft==false) {
//Debug.Log (firep.position.x);
//Instantiate (leftLaser, firep.position*1.29f, Quaternion.identity);
StartCoroutine( DelayMethodLeftInstantiate());
}
if (robotRight == true&&isLaserLeft==false) {
StartCoroutine( DelayMethodRightInstantiate());
//Instantiate (RightLaser, firep.position, Quaternion.identity);
}
anim.SetBool ("isAttack", isAttacking);
isLaserLeft = true;
}
public void LaserAttackEnd(){
isAttacking = false;
attackStill = false;
anim.SetBool ("isAttackStill",attackStill);
anim.SetBool ("isAttack", isAttacking);
isLaserLeft = false;
}
和协程代码(左右两个):
IEnumerator DelayMethodLeftInstantiate(){
yield return new WaitForSeconds(1);
Instantiate (leftLaser, firep.position, Quaternion.identity);
}
IEnumerator DelayMethodRightInstantiate(){
yield return new WaitForSeconds(1);
Instantiate (RightLaser, firep.position, Quaternion.identity);
}
请帮忙,对不起,我缺乏知识......伙计们我是菜鸟!
可能有两种方法可以解决您的问题。
首先,如果按钮被释放,您可以停止协程。
您可以使用 IPointerUpHandler(我假设您从 UI 按钮调用该方法)
public class InputHandler, MonoBehaviour, IPointerUpHandler
{
IEnumerator coroutine = null;
public void LaserAttack(){
isAttacking = true;
this.coroutine = AttackStillCoroutine ()
StartCoroutine (this.coroutine);
// More code
}
public void OnPointerUp(PointerEventData eventData)
{
if(this.isAttacking == true && this.coroutine != null ){
StopCoroutine(this.coroutine);
this.coroutine = null;
}
}
}
我推荐的另一种方法是使用 AnimationEvent。您可以为动画放置一个方法委托,以便在它到达时调用该方法。
我将粘贴 link,因为它比代码更直观:
https://docs.unity3d.com/Manual/animeditor-AnimationEvents.html
我的玩家Robot
在我点击攻击按钮时正在攻击,如下所示。我正在使用 coroutine
等待一段时间,然后实例化激光形式 firepos
位置。
请看下面的内容gif
,以便更清楚地理解:
现在如果你看到上面的 gif
你可以看到如果我按住 攻击 按钮玩家正在做他的动画并且根据 waitForSeconds
coroutine
一秒后激光发射。到现在都很好!
但是当我在短时间内突然多次点击按钮时,动画 不播放(我能理解..这很明显!) 但就像上面的情况一样过了一段时间(由于 waitforseconds
)并且由于 我没有点击 相同数量的激光发射 我不想 !
我想要的是当我点击并按住攻击按钮时,动画应该完成,然后激光应该发射。
AND
如果我在短时间内多次点击它,激光(这是一个正在实例化的预制件)应该不会发射。
只有当动画完成时激光才会发射。
现在我想不通了!任何人都可以帮忙。
检查器中的按钮(攻击):
我在 OnpointerDown
和 up
功能代码:
public void LaserAttack(){
isAttacking = true;
StartCoroutine (AttackStillCoroutine ());
if (robotLeft == true&&isLaserLeft==false) {
//Debug.Log (firep.position.x);
//Instantiate (leftLaser, firep.position*1.29f, Quaternion.identity);
StartCoroutine( DelayMethodLeftInstantiate());
}
if (robotRight == true&&isLaserLeft==false) {
StartCoroutine( DelayMethodRightInstantiate());
//Instantiate (RightLaser, firep.position, Quaternion.identity);
}
anim.SetBool ("isAttack", isAttacking);
isLaserLeft = true;
}
public void LaserAttackEnd(){
isAttacking = false;
attackStill = false;
anim.SetBool ("isAttackStill",attackStill);
anim.SetBool ("isAttack", isAttacking);
isLaserLeft = false;
}
和协程代码(左右两个):
IEnumerator DelayMethodLeftInstantiate(){
yield return new WaitForSeconds(1);
Instantiate (leftLaser, firep.position, Quaternion.identity);
}
IEnumerator DelayMethodRightInstantiate(){
yield return new WaitForSeconds(1);
Instantiate (RightLaser, firep.position, Quaternion.identity);
}
请帮忙,对不起,我缺乏知识......伙计们我是菜鸟!
可能有两种方法可以解决您的问题。
首先,如果按钮被释放,您可以停止协程。
您可以使用 IPointerUpHandler(我假设您从 UI 按钮调用该方法)
public class InputHandler, MonoBehaviour, IPointerUpHandler
{
IEnumerator coroutine = null;
public void LaserAttack(){
isAttacking = true;
this.coroutine = AttackStillCoroutine ()
StartCoroutine (this.coroutine);
// More code
}
public void OnPointerUp(PointerEventData eventData)
{
if(this.isAttacking == true && this.coroutine != null ){
StopCoroutine(this.coroutine);
this.coroutine = null;
}
}
}
我推荐的另一种方法是使用 AnimationEvent。您可以为动画放置一个方法委托,以便在它到达时调用该方法。
我将粘贴 link,因为它比代码更直观:
https://docs.unity3d.com/Manual/animeditor-AnimationEvents.html