我有一个标记为“玩家”的立方体,它需要在特定位置跳跃,由空游戏对象表示。
我想做一个积分系统,这就是为什么我把其他游戏对象放在一些标记为“硬币”的地方
立方体有一个动画,每次移动到另一个点时都会播放,该点可以通过在其上单击鼠标来识别。
当我跳到硬币位置时,我希望硬币对象被销毁。
我使用这个函数:
void OnTriggerEnter(Collider col){
if (col.gameObject.tag == "Coin") {
Debug.Log ("Plm");
stroy (col.gameObject);
}
问题是,当我在硬币位置跳跃时,什么也没有发生。
我在两个对象上都有盒子碰撞器,并且在两个对象上都检查了 OnTrigger
。
最佳答案
1) 确保一个游戏对象有刚体
拥有OnTriggerEnter
碰巧您至少需要两个对象之一才能拥有 RigidBody
附在上面。
2) 静态触发对撞机
具有碰撞器和 isTrigger
的对象检查但没有 rigibBody 被认为是 Static Trigger Collider
这是From Unity Documentation:
This is a GameObject that has a Collider but no Rigidbody. Static colliders are used for level geometry which always stays at the same place and never moves around. Incoming rigidbody objects will collide with the static collider but will not move it.
这种类型的碰撞器交互非常适合您的硬币,因为它们不会移动并保持静止。
3)刚体触发对撞机
下一类对撞机交互是 Rigidbody Trigger Collider
这是:
This is a GameObject with a Collider and a normal, non-kinematic Rigidbody attached. Rigidbody colliders are fully simulated by the physics engine and can react to collisions and forces applied from a script. They can collide with other objects (including static colliders)
这种类型的对撞机是您 player
上所需要的正如它在 Unity 文档中所说,它将能够与 Static trigger collider
发生冲突。 .
重要观察:
拥有IsTrigger
在您的玩家对撞机上设置为 true 将导致您的玩家不作为固体对象响应
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.
这可能不是您的最佳解决方案,因为大多数时候您希望您的玩家能够与地面等其他物体发生碰撞。所以你可以设置 isTrigger
为 false 并在你的玩家身上设置一个刚体,如果它们被设置为 Static Trigger Collider
,它会对硬币使用react.
如果你想要一个简单的方法来知道什么可以与什么发生碰撞Read the colliders documentation并查看以更简单的方式显示它的图表。
关于c# - 为什么我没有碰撞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42982317/