在Unity3D中点击Play后,如何使Rigidbody2D.Simulated保持真实?

时间:2019-04-30 13:31:01

标签: c# visual-studio unity3d

我正在制作《 Flappy Bird》风格的游戏和Rigidbody2D.simset,如果按Play测试应用程序后是否设置为false。我必须在哪里放置rigidbody.simulated = true以确保它保持这种方式,以后我可以将其关闭?

如果我将其放在void Update()中,则void OnTriggerEnter2D(Collider2D collision)将无法正常工作。

Rigidbody2D rigidbody;

Quaternion downRotation;
Quaternion forwardRotation;

void Start()
{
    rigidbody = GetComponent<Rigidbody2D>();

    //it doesn't work if I put it here

    downRotation = Quaternion.Euler(0,0,-90);
    forwardRotation = Quaternion.Euler(0, 0, 35);


}

void Update()
{

    //if I put it here then void OnTriggerEnter2D() will not work

    if (Input.GetMouseButtonDown(0))
    {
        transform.rotation = forwardRotation;
        rigidbody.AddForce(Vector2.up * tapForce, ForceMode2D.Force);
    }
    transform.rotation = Quaternion.Lerp(transform.rotation, downRotation, tiltSmooth * Time.deltaTime);
}

void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.gameObject.tag == "ScoreZone")
    {
        // ToDo: register a score event
        // Play a sound
    }

    if (collision.gameObject.tag == "DeadZone")
    {
        rigidbody.simulated = false;
        //register a dead event
        //play a sound
    }
}

}

2 个答案:

答案 0 :(得分:0)

您当前的代码没有问题。 rigidbody.simulated仅需要在检查器中标记为true。

问题似乎是重叠的对撞机,该对撞机在开始时立即触发该代码。

if (collision.gameObject.tag == "DeadZone")
    {
        rigidbody.simulated = false;
//put the debug here
    }

尝试放置Debug.log(collision.gameObject.name);来找出导致该问题的gameObject。

答案 1 :(得分:0)

从包含Debug.Log(collision.gameObject.name)的对象中删除BoxCollider2D;要求,然后再次添加它们。现在就像魅力一样。