物理忽略以避免碰撞
Physics ignore to avoid collision
我知道使用 Physics.Collision 来避免两个特定对象之间的碰撞。但我希望他们的盒子碰撞器处于活动状态,以便它检测到它们正在接触。有办法实现吗?
另外,在我使用 Physics.Collision() 禁用碰撞后,我可以重新激活对象之间的碰撞吗?
Physics.IgnoreCollision(obj2.GetComponent<Collider>(), this.GetComponent<Collider>());
在 Unity 菜单栏中,转到 Edit > Project Settings
,然后 select 物理类别打开物理 window。你会发现一个矩阵,在这里你可以取消选中 2 层之间的碰撞。如果你把 Player 放在一个图层中,而你不想碰撞的物体放在另一个图层中,在矩阵中你取消选中该框,所有东西都会保持他的属性并且玩家不会与之碰撞。
ignore
: Whether or not the collisions between the two colliders should be ignored or not.
默认为 true
。
要重新启用您之前禁用的碰撞,只需传入 false
。
Physics.IgnoreCollision(obj2.GetComponent<Collider>(), this.GetComponent<Collider>(), false);
如果您不希望对象发生碰撞但在它们相互接触时进行注册,则对它们进行 Trigger.
如果对象是触发器,则它不会对碰撞做出反应,但会触发 OnTriggerEnter
。
The scripting system can detect when collisions occur and initiate actions using the OnCollisionEnter function. However, you can also use the physics engine
simply to detect when one collider enters the space of another without creating a collision. A collider configured as a Trigger (using the Is Trigger property) does not behave as a solid object and will simply allow other colliders to pass through. When a collider enters its space, a trigger will call the OnTriggerEnter function on the trigger object’s scripts
.
我知道使用 Physics.Collision 来避免两个特定对象之间的碰撞。但我希望他们的盒子碰撞器处于活动状态,以便它检测到它们正在接触。有办法实现吗?
另外,在我使用 Physics.Collision() 禁用碰撞后,我可以重新激活对象之间的碰撞吗?
Physics.IgnoreCollision(obj2.GetComponent<Collider>(), this.GetComponent<Collider>());
在 Unity 菜单栏中,转到 Edit > Project Settings
,然后 select 物理类别打开物理 window。你会发现一个矩阵,在这里你可以取消选中 2 层之间的碰撞。如果你把 Player 放在一个图层中,而你不想碰撞的物体放在另一个图层中,在矩阵中你取消选中该框,所有东西都会保持他的属性并且玩家不会与之碰撞。
ignore
: Whether or not the collisions between the two colliders should be ignored or not.
默认为 true
。
要重新启用您之前禁用的碰撞,只需传入 false
。
Physics.IgnoreCollision(obj2.GetComponent<Collider>(), this.GetComponent<Collider>(), false);
如果您不希望对象发生碰撞但在它们相互接触时进行注册,则对它们进行 Trigger.
如果对象是触发器,则它不会对碰撞做出反应,但会触发 OnTriggerEnter
。
The scripting system can detect when collisions occur and initiate actions using the OnCollisionEnter function. However, you can also use the physics engine simply to detect when one collider enters the space of another without creating a collision. A collider configured as a Trigger (using the Is Trigger property) does not behave as a solid object and will simply allow other colliders to pass through. When a collider enters its space, a trigger will call the OnTriggerEnter function on the trigger object’s scripts .