0

我正在编写一个 2d 平台游戏。在游戏中,有盟友和敌人走向对方。如果它们相互碰撞,它们自己的计时器脚本会开始倒计时,并且生命值会随着时间的推移而减少。如果角色生命值为零,我会销毁该游戏对象。我为每个字符添加了一些布尔值来检测它们是否相互碰撞。在碰撞两个游戏对象时,如果一个破坏了,其他碰撞的游戏对象仍然连续碰撞,尽管没有碰撞对象。发生这种情况只是其他碰撞对象被破坏。

public float  setSpeed;
public bool enemyColliding;

float speed;

void Start () {

}

// Update is called once per frame
void Update () {
    speed= setSpeed;
    GetComponent<Rigidbody2D> ().velocity = new Vector2 (speed,GetComponent<Rigidbody2D>().velocity.y);
    if (enemyColliding) {
        attackAnimation ();
    } 
    else
    {
       walkAnimation();
    }
}

void OnTriggerEnter2D(Collider2D coll)
{
    if (coll.gameObject.tag == "dusman" /*enemy*/ ) {
        enemyColliding= true;
    }

}
void OnTriggerExit2D(Collider2D coll)
{
    if (coll.gameObject.tag=="dusman" /*enemy*/) {
        enemyColliding= false;
    }
}
void attackAnimation()
{
    Animator animator = this.gameObject.GetComponent<Animator> ();
    animator.runtimeAnimatorController = Resources.Load ("AllyWr2AttackAnim") as RuntimeAnimatorController;
}
void walkAnimation ()
{
    Animator animator = this.gameObject.GetComponent<Animator> ();
    animator.runtimeAnimatorController = Resources.Load ("AllyWr2WalkAnim") as RuntimeAnimatorController;
}

任何帮助请...

4

1 回答 1

1
void OnTriggerExit2D(Collider2D coll)
{
   enemyColliding= false;
}

尝试这个

于 2016-09-27T11:50:22.573 回答