首页 > 解决方案 > 在 Unity3D 中点击 Play 后如何使 Rigidbody2D.Simulated 保持真实?

问题描述

我正在制作 Flappy Bird 风格的游戏,并且 Rigidbody2D.simulated 在按下 Play 测试应用程序后设置 ifself 为 false。我必须把它放在哪里rigidbody.simulated = true以确保它保持这种状态并且我可以稍后将其关闭?

如果我将其置于 void 中,Update()则 voidOnTriggerEnter2D(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
    }
}

}

标签: c#visual-studiounity3d

解决方案


您当前的代码没有问题。rigidbody.simulated只需要在inspector中标记为true。

问题似乎是一个重叠的对撞机,它一开始就会触发此代码。

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

尝试放置一个Debug.log(collision.gameObject.name);以找出导致该游戏对象的原因。


推荐阅读