c# - 为什么我没有碰撞?

标签 c# unity3d collision

我有一个标记为“玩家”的立方体,它需要在特定位置跳跃,由空游戏对象表示。

我想做一个积分系统,这就是为什么我把其他游戏对象放在一些标记为“硬币”的地方

立方体有一个动画,每次移动到另一个点时都会播放,该点可以通过在其上单击鼠标来识别。
当我跳到硬币位置时,我希望硬币对象被销毁。

我使用这个函数:

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并查看以更简单的方式显示它的图表。

enter image description here

关于c# - 为什么我没有碰撞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42982317/

相关文章:

javascript - 无法在 JS (box2d) 处理中删除正文

c# - 在 C# 中验证不同的日期格式

c# - 从私有(private)void函数c#中的另一个类访问变量

安卓和引擎 : handling Collission Detection properly

c# - 以概率生成 true 或 false bool 值

java - 在 com.unity3d.player 中找不到 UnityPlayerActivity 类

javascript - p5.j​​s 中的碰撞处理

c# - 如何在 ItemsControl 中仅显示集合中的几个项目 - Silverlight

c# - 将 C# 泛型方法转换为 Objective-C

C# 访问 IEnumerable 集合