c# - 在 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
}
}
}
解决方案
您当前的代码没有问题。rigidbody.simulated
只需要在inspector中标记为true。
问题似乎是一个重叠的对撞机,它一开始就会触发此代码。
if (collision.gameObject.tag == "DeadZone")
{
rigidbody.simulated = false;
//put the debug here
}
尝试放置一个Debug.log(collision.gameObject.name);
以找出导致该游戏对象的原因。
推荐阅读
- javascript - 确定鼠标单击的 X 坐标
- dart - 如何拆分字符串并将每一行放入 Dart 的元素数组中?
- r - R:在列表中累积乘以df的行并从操作中返回一个列表
- javascript - 打字稿HTML显示
- jquery - Jquery Append On click 函数表
- android - Android Studio 没有写权限
- crystal-reports-xi - Crystal Reports:只显示我想要的值
- azure-active-directory - 使用 InTune 锁定 Windows PC 以阻止 cmd 和 regedit 等
- javascript - 更改组件 React Web
- angular - 如何强制以角度反应形式的字段重新运行验证