如果其他游戏对象破坏,Unity 2D 碰撞不会停止
Unity 2D collision not stopping if other gameobject destroys
我正在编写一个 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;
}
请帮忙...
void OnTriggerExit2D(Collider2D coll)
{
enemyColliding= false;
}
试试这个
我正在编写一个 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;
}
请帮忙...
void OnTriggerExit2D(Collider2D coll)
{
enemyColliding= false;
}
试试这个