【问题标题】:Unity 2D collision not stopping if other gameobject destroys如果其他游戏对象破坏,Unity 2D 碰撞不会停止
【发布时间】:2023-03-31 13:44:01
【问题描述】:

我正在编写一个 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;
}

请帮忙...

【问题讨论】:

    标签: unity3d 2d collision destroy collider


    【解决方案1】:
    void OnTriggerExit2D(Collider2D coll)
    {
       enemyColliding= false;
    }
    

    试试这个

    【讨论】:

    • 感谢您的回答。有不止一个不同的游戏对象。如果我这样做,它会检测到其他人。
    • 如果我是对的,当您删除 hp
    • 另一种方法是改变游戏对象在游戏场外的位置,然后将其删除。黑客)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多