在OnCollisionEnter()时使isKinematic为false?

时间:2015-10-24 12:01:03

标签: c# unity3d

我的脚本有问题,我想让rb.isKinematic在检测到碰撞时从true变为false。使用这个脚本,当发生假定的碰撞时没有任何反应意味着什么rb.Kinematic = false不起作用...问题是首先无法检测到碰撞,因为rb.isKinematic最初是真的(在我的其他脚本中) )!我使用Debug.Log检查了这个以检测冲突。我该如何解决这个问题?

string lastTagCollided = "";
public Rigidbody rb;

void OnCollisionEnter(Collision col)
{
    if(col.gameObject.tag == "Pentagon" && lastTagCollided == "Pyramid")
    {
        rb = GetComponent<Rigidbody>();
        rb.isKinematic = false;
        transform.DetachChildren();
        Destroy (GameObject.FindWithTag("Sphere"));
    }

    if(col.gameObject.tag == "Pyramid" && lastTagCollided == "Pentagon")
    {
        rb = GetComponent<Rigidbody>();
        rb.isKinematic = false;
        transform.DetachChildren();
        Destroy (GameObject.FindWithTag("Sphere"));
    }

    lastTagCollided = col.gameObject.tag;
}

如果你好奇,我的另一个剧本:

void OnCollisionEnter(Collision col) 
    {
    if (col.gameObject.tag == "Pyramid" || col.gameObject.tag == "Pentagon") 
    {
        rb = GetComponent<Rigidbody>();
        rb.isKinematic = true;
        gameObject.transform.SetParent (col.gameObject.transform);
    }
}

1 个答案:

答案 0 :(得分:0)

我建议将这些代码更改为

string lastTagCollided = "";
public Rigidbody rb;

void OnCollisionEnter(Collision col)
{
    Debug.Log("Debug 1: Collision " + col.gameObject.tag);
    if(col.gameObject.tag == "Pentagon" && lastTagCollided == "Pyramid")
    {
    Debug.Log("Debug 2: col.gameObject.tag == \"Pentagon\" && lastTagCollided == \"Pyramid\" results true - Collision " + col.gameObject.tag);
        rb = GetComponent<Rigidbody>();
        rb.isKinematic = false;
        transform.DetachChildren();
        Destroy (GameObject.FindWithTag("Sphere"));
    }

    if(col.gameObject.tag == "Pyramid" && lastTagCollided == "Pentagon")
    {
    Debug.Log("Debug 3: col.gameObject.tag == \"Pyramid\" && lastTagCollided == \"Pentagon\" results true - Collision " + col.gameObject.tag);
        rb = GetComponent<Rigidbody>();
        rb.isKinematic = false;
        transform.DetachChildren();
        Destroy (GameObject.FindWithTag("Sphere"));
    }

    lastTagCollided = col.gameObject.tag;
}
}

void OnCollisionEnter(Collision col) 
    {
    Debug.Log("Debug 1: Collision " + col.gameObject.tag);
        if (col.gameObject.tag == "Pyramid" || col.gameObject.tag == "Pentagon") 
        {
            Debug.Log("Debug 2: col.gameObject.tag == \"Pyramid\" || col.gameObject.tag == \"Pentagon\" results true - Collision " + col.gameObject.tag);
            rb = GetComponent<Rigidbody>();
            rb.isKinematic = true;
            gameObject.transform.SetParent (col.gameObject.transform);
        }
}

这将为您提供更好的日志,以查看是否发生了碰撞。

您可能还想记录rb.isKinematic之前和之后的内容,如果您无法从中找出它,请使用Unity控制台的输出编辑您的帖子,并附上您可以提供的更多信息。