当isTrigger为true时,碰撞对象会弹起,而不是通过 [英] Colliding objects bouncing off when isTrigger is true instead of passing through

查看:149
本文介绍了当isTrigger为true时,碰撞对象会弹起,而不是通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经遍历了一些游戏对象,以在发生某些碰撞之前设置isTrigger属性,但是尽管该属性为true,但仍然与该对象发生碰撞.请参见下面的相关代码:

I have looped through some game objects to set the isTrigger property before a certain collision occurs but though the property is true collision with the object still occurs. Please see the relevant code below:

    void OnCollisionEnter2D(Collision2D col)    {

    if (col.gameObject.tag == "object1") {
         for (int i = 0; i < badGuys.Count; i++)    {
         badGuys[i].getBadGuyGameObject().GetComponent<Collider2D>().isTrigger = true;
      }

      }

    else if (col.gameObject.tag == "object2") {

       // collision with object1 always occurs before object2, though isTrigger is true the colliding object doesn't pass through the badGuys game objects, it bounces off on collision
      }
   else if (col.gameObject.tag == "object3") {
      for (int i = 0; i < badGuys.Count; i++)   {
         badGuys[i].getBadGuyGameObject().GetComponent<Collider2D>().isTrigger = false;
      }
      }
    else if (col.gameObject.tag == "badGuy") {

      }

}

与对象1的碰撞总是在对象2之前发生,尽管isTrigger为true,碰撞对象不会通过badGuys游戏对象,而是在碰撞时反弹.我该如何解决?

collision with object1 always occurs before object2, though isTrigger is true the colliding object doesn't pass through the badGuys game objects, it bounces off on collision. How can I solve this?

更新1

我刚刚意识到,根据我停止游戏的时间,isTrigger值在检查器中为true还是false.例如,如果我在Object1碰撞后停止游戏,则isTrigger为true,而当我与object3碰撞后停止游戏时,它为false.这种不一致使调试非常麻烦.这是bug还是什么?

I just realized that the isTrigger value is true or false in the inspector depending on the time I stop the game. For example if I stop the game after Object1 collision the isTrigger is true and when I stop the game after collision with object3 it false. This inconsistency makes debugging very cumbersome. Is this a bug or something?

更新2

基于Joe Blow和Agustin0987的建议,我使用了Collider2Denabled属性而不是isTrigger.我还删除了OnCollisionEnter2D中的几乎所有代码,以得到一个简单的测试方案.请参见下面的代码:

Based on recommendation from Joe Blow and Agustin0987 I used the enabled property of Collider2D instead of isTrigger. I also removed virtually all the code in OnCollisionEnter2D to get a simple test scenario. Please see code below:

void OnCollisionEnter2D(Collision2D col)    {

    if (col.gameObject.tag == "object1") {

        if (badGuys[4].getBadGuyGameObject().GetComponent<Collider2D>().enabled = false) {
            badGuys[4].getBadGuyGameObject().GetComponent<Collider2D>().enabled = true;
            Debug.Log ("CHANGED TO TRUE");
            Debug.Log ("bad guy  collider enabled is " + badGuys[4].getBadGuyGameObject().GetComponent<Collider2D>().enabled);

        }


        else if (badGuys[4].getBadGuyGameObject().GetComponent<Collider2D>().enabled = true) {
            badGuys[4].getBadGuyGameObject().GetComponent<Collider2D>().enabled = false;
            Debug.Log ("CHANGED TO FALSE");
            Debug.Log ("bad guy collider enabled is " + badGuys[4].getBadGuyGameObject().GetComponent<Collider2D>().enabled);


        }
    }
    else if (col.gameObject.tag == "badGuy") {
       Debug.Log("collided with bad guy"); // DOESN'T OCCUR
    }

}

我决定不进行循环测试,而是决定进行一次测试.尽管Log打印false,但总是满足enabled的情况,而enabled总是满足,而enabledfalseif永远不会被满足.在检查器中,针对坏人的Box Collider2D始终未选中.与badGuy的碰撞永远不会发生.根据我简单的场景代码,我认为它应该可以工作.

Instead of looping through this time, I decided to test for one. The else if is where enabled is true always satisfied though the Log prints false and the if where enabled is false is never satisfied. In the inspector, the Box Collider2D for bad guys is always unchecked. Collision with the badGuy never occurs. Based on my simple scenario code I thought it should be working.

推荐答案

永远,永远不会

使用"else if".

use "else if".

简单,永远,永远-永远-输入"else if"一辈子直到死.

Simply, never, ever - ever - type "else if" for the rest of your life, until you die.

首先,删除所有代码,并用以下代码替换:

In the first instance, delete all your code and replace it with this:

void OnCollisionEnter2D(Collision2D col)
{
Debug.Log("We will deal with this ......... " +col.gameObject.name);
if (col.gameObject.tag == "object1")
    {
    Debug.Log("\t handling 'object' type issue");
    HandleObjectIssue(col)
    return; //NOTE HERE
    }
if (col.gameObject.tag == "object1")
    {
    Debug.Log("\t handling 'bad guy' type issue");
    HandleBadGuyIssue(col)
    return; //NOTE HERE
    }
}

private void HandleObjectIssue(Collision2D col)
{
Debug.Log("'\t\t object', I'm dealing with this .. " +col.gameObject.name);
}


private void HandleBadGuyIssue(Collision2D col)
{
Debug.Log("\t\t 'badguy', I'm dealing with this .. " +col.gameObject.name);
}

广泛运行,然后进行单元测试"它.如果愿意,可将其转换回(使用该代码)为循环".查看所有项目的方法.无论如何,都要进行广泛的测试.

Run that extensively and "unit test" it. If you like, convert back (USING THAT CODE) to a "loop" method to look at all items. In any event, test that extensively.

-

第二,添加此例程..

Secondly, add this routine ..

private void ToggleColliderOnGameObject( GameObject gg )
{
Debug.Log("I'm supposed to toggle the collider on: " +gg.name);
Collider2D { or whatever } col = gg.GetComponent<Collider2D>();
bool currentState = GetComponent<Collider2D>().enabled;

Debug.Log("\t it is currently: " +currentState);

bool newState = ! currentState;
col.enabled = newState;

Debug.Log("\t\t but now it is: " +newState);
}

...并开始使用它进行单元测试.因此,您将获得如下代码:

... and start unit testing with it. So you will have code like:

 ToggleColliderOnGameObject( badGuys[4].getBadGuyGameObject() );

例如,尝试一些类似的事情:

For example, try some things like this:

void Start()
    {
    InvokeRepeating("teste", 1f, 1f);
    }
private void Teste()
    {
    ToggleColliderOnGameObject( badGuys[4].getBadGuyGameObject() );
    }

在进行了一段时间的测试和单元测试之后,您将能够继续前进.

After experimenting with that for some time and unit testing, you will be able to move on.

请注意,除了其他问题外,您的例程"getBadGuyGameObject"可能会(例如)返回错误的内容-或某些类似的问题.

Note that apart from other problems, it is likely that (for example) your routine "getBadGuyGameObject" is returning the wrong thing - or some similar problem.

编辑:

删除 badGuy = badGuyPrefab;.这是您注释中链接中Programmer代码的错误.

Remove badGuy = badGuyPrefab; from your public void createBadGuy(GameObject badGuyPrefab, BadGuyType badGuyType, Vector3 badGuyPosition) function. This was a mistake in Programmer's code from the link in your comment.

这篇关于当isTrigger为true时,碰撞对象会弹起,而不是通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆