我正在编写一个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;
}
任何帮助请...
答案 0 :(得分:1)
void OnTriggerExit2D(Collider2D coll)
{
enemyColliding= false;
}
试试这个