如何防止RigidBody通过其他对撞机

Ash*_*ria 5 rigid-bodies unity-game-engine game-physics unityscript

我有一枚硬币RigidBody和带有撞机盒的墙壁。为了试用,我将以下代码应用于硬币。

private void OnMouseDown() 
{
    rigidbody.AddForce(30.0f, 0f, 5.0f, ForceMode.Impulse);
}
Run Code Online (Sandbox Code Playgroud)

但是,有时硬币会穿过墙壁,但是当我将速度从30点提高到50点时,硬币会在第一次点击时穿过墙壁。我搜索了很多东西,但除了DontGoThroughThings脚本对我不起作用或者我真的不知道如何使用它外,什么都没有。

我一直在硬币上添加连续的动态效果,在墙壁上添加连续的碰撞检测功能,但仍然无法正常工作。

小智 6

the problem with physics collision detection is that sometimes when the speed of an object is too high (in this case as a result of a force added to the rigid body) the collision wont be detected. The reason is that the code you are executing is running at x ammount of steps per seconds so sometimes the rigidbody will go through the collider between one step to another. Lets say you have a ball at 100 miles per hour and a wall that is 1 feet wide, the code will move the ball a determined ammount of feets everytime the code is runned according the physics of it, so the movement of the ball is virtualized but its not a real physical movement so it can happen that from one step to another the ball can move from point a to b and the wall is between those points and as a result the collision will not be detected.

Possible Solutions.

You have a simple solution that wont be as accurate as it should be and a harder one that will be perfectly accurate.

The solution number one will be increasing the colliders size, according to the max speed your coin can get, that way the collider will be big enough so that the collision wont be missed between one frame to another.

The second and harder solution will be adding an auxiliar to your collision detection, some kind of a security check. For example using physical raycast. You can set a raycast to forward direction and determine if that object is an inminent collision, if it does and once the object isnt being collided by the raycast anymore then your collision detection had failed that way you have your auxiliar to confirm that and call the collision state.

我希望它对我的英语有所帮助和抱歉。如果您不太了解它,我可以帮助您编写代码,但我需要您项目中的一些代码。