Sphere Collider 球体碰撞器

优质
小牛编辑
134浏览
2023-12-01

The Sphere Collider is a basic sphere-shaped collision primitive.

球体碰撞器是一个基本的球体形状的原型碰撞器。

A pile of Sphere Colliders 一堆球体碰撞器
A pile of Sphere Colliders 一堆球体碰撞器

Properties 属性

  • Material 材质 Reference to the Physic Material that determines how this Collider interacts with others.
    引用何种物理材质决定了他和其他对象如何作用。
  • Is Trigger 是否碰撞器 If enabled, this Collider is used for triggering events, and is ignored by the physics engine.
    如激活,此碰撞器用于触发事件,并且被物理引擎忽略。
  • Radius 半径 The size of the Collider. 碰撞器的大小
  • Center 中心 The position of the Collider in the object's local space.
    碰撞器在对象本地坐标中的位置

Details 细节

The Sphere Collider can be resized to uniform scale, but not along individual axes. It works great for falling boulders, ping pong balls, marbles, etc.

球体碰撞器在三维上可以均等地调节大小,但不能只改变某一维(保持球体)。适合用于落石、乒乓球、弹球等等。

A standard Sphere Collider 一个标准的球体碰撞器

A standard Sphere Collider 一个标准的球体碰撞器

Colliders work with Rigidbodies to bring physics in Unity to life. Whereas Rigidbodies allow objects to be controlled by physics, Colliders allow objects to collide with each other. Colliders must be added to objects independently of Rigidbodies. A Collider does not necessarily need a Rigidbody attached, but a Rigidbody must be attached in order for the object to move as a result of collisions.

碰撞器作用于刚体使Unity中的物理作用生效。因为刚体允许对象被物理系统控制。碰撞器使对象之间产生碰撞作用。碰撞器必须独立于刚体加在对象上。碰撞器并不一定需要附加刚体,但是要使移动物体具有碰撞效果必须附加刚体。

When a collision between two Colliders occurs and if at least one of them has a Rigidbody attached, three collision messages are sent out to the objects attached to them. These events can be handled in scripting, and allow you to create unique behaviors with or without making use of the built-in NVIDIA PhysX engine.

当两个碰撞器间产生碰撞并且其中至少一个附加了刚体时,三个碰撞信息会发送给附加他们的对象,这些事件可以被脚本处理,而且允许用户可以选择自己写脚本或使用内置的NVIDIA PhysX引擎创建唯一的行为。

Triggers 触发器

An alternative way of using Colliders is to mark them as a Trigger, just check the IsTrigger property checkbox in the Inspector. Triggers are effectively ignored by the physics engine, and have a unique set of three trigger messages that are sent out when a collision with a Trigger occurs. Triggers are useful for triggering other events in your game, like cutscenes, automatic door opening, displaying tutorial messages, etc. Use your imagination!

一个可供选择的碰撞器使用方法是标记他们为触发器,仅需要选中检视面板中的IsTrigger属性选择框。触发器不受物理引擎控制,当和一个触发器发生碰撞时会发出三个独特的触发信息。触发器用于触发你游戏中的其他事件,比如过场动画、自动门开启、显示教程信息等等,发挥你的想象力吧!

Be aware that in order for two Triggers to send out trigger events when they collide, one of them must include a Rigidbody as well. For a Trigger to collide with a normal Collider, one of them must have a Rigidbody attached. For a detailed chart of different types of collisions, see the collision action matrix in the Advanced section below.

当然也应该意识到为了使两个触发器碰撞时发出碰撞事件,其中一个必须包含刚体。一个触发器和一个普通碰撞器碰撞,其中之一必须附加刚体。想了解不同类型碰撞的详细情况,可以参考下边进阶部分中的碰撞行为矩阵。

Friction and bouncyness 摩擦力和弹力

Friction, bouncyness and softness are defined in the Physic Material. The Standard Assets contain the most common physics materials. To use one of them click on the Physic Material drop-down and select one, eg. Ice. You can also create your own physics materials and tweak all friction values.

摩擦力、弹力和柔软度由物理材质中定义。标准资源( Standard Assets)中包含了大部分常见的物理材质。要使用只需点击物理材质上的下拉箭头然后选中一个,比如冰。你也可以创建你自己的物理材质并调整摩擦力的大小。

Compound Colliders 复合碰撞器

Compound Colliders are combinations of primitive Colliders, collectively acting as a single Collider. They come in handy when you have a complex mesh to use in collisions but cannot use a Mesh Collider. To create a Compound Collider, create child objects of your colliding object, then add a primitive Collider to each child object. This allows you to position, rotate, and scale each Collider easily and independently of one another.

复合碰撞器是组合原型碰撞器,共同扮演单个碰撞器的角色。当你在碰撞器上使用了一组复杂的网格而网格碰撞器不能用时,这是很好的选择。要创建复合碰撞器,只需给你的碰撞器对象创建子对象,然后为每个子对象添加原型碰撞器。这就允许你轻易地移动、旋转或者伸缩每个碰撞器,并使他们互相之间互不影响。

A real-world Compound Collider setup 一个真实复合碰撞器设置
A real-world Compound Collider setup 一个真实复合碰撞器设置

In the above picture, the environment has a Mesh Collider attached. Mesh Colliders work the best for terrain or environments made from irregular shapes. The gun_model GameObject has a Rigidbody attached, and multiple primitive Colliders as child GameObjects. When the Rigidbody parent is moved around by forces, the child Colliders move along with it. The primitive Colliders will collide with the environment's Mesh Collider, and the parent Rigidbody will alter the way it moves based on forces being applied to it and how its child Colliders interact with other Colliders in the Scene.

上图中,环境附加了一个网格碰撞器。网格碰撞器用于由不规则形状构成的地形或环境十分合适。枪模型游戏对象附加了刚体和多个原型碰撞器子对象。当父级刚体受力移动时,子级碰撞器也随之一起移动。原型碰撞器会和环境网格碰撞器碰撞,同时父级刚体会因子级碰撞器和场景中其他碰撞器作用产生的力改变移动路径。

Mesh Colliders can't normally collide with each other. If a Mesh Collider is marked as Convex, then it can collide with another Mesh Collider. The typical solution is to use primitive Colliders for any objects that move, and Mesh Colliders for static background objects.

网格碰撞器通常无法互相碰撞,如果一个网格碰撞器标记为凸起的(Convex),那么它可以和其他碰撞器碰撞。典型的解决方法是在移动对象上使用原型碰撞器而在静态的背景对象上使用网格碰撞器。

Hints 提示

  • To add multiple Colliders for an object, create child GameObjects and attach a Collider to each one. This allows each Collider to be manipulated independently.
    要给一个对象添加多个碰撞器,应该创建子对象并给每个子对象添加一个碰撞器。这使每个碰撞器可以被单独操作。
  • You can look at the gizmos in the Scene View to see how the Collider is being calculated on your object.
    可以通过场景视图(Scene View )中的线框(gizmos)来查看碰撞器在你的对象上的预期效果。
  • Colliders do their best to match the scale of an object. If you have a non-uniform scale (a scale which is different in each direction), only the Mesh Collider can match completely.
    碰撞器尽量匹配对象的比例。如果有一组不规则伸缩(每一维度上的比例都不相同),只能使用网格碰撞器才能完全匹配。
  • If you are moving an object through its Transform component but you want to receive Collision/Trigger messages, you must attach a Rigidbody to the object that is moving.
    如果你想接收通过Transform组件移动的对象的碰撞/触发信息,你必须给移动对象附加刚体。
  • If you make an explosion, it can be very effective to add a rigidbody with lots of drag and a sphere collider to it in order to push it out a bit from the wall it hits.
    如果你制作一个爆炸,添加带有很多阻力和一个球体碰撞器为了使他碰到墙体,产生有效的爆炸。

Advanced 进阶

Collider combinations碰撞器组合

There are numerous different combinations of collisions that can happen in Unity. Each game is unique, and different combinations may work better for different types of games. If you're using physics in your game, it will be very helpful to understand the different basic Collider types, their common uses, and how they interact with other types of objects.

Unity中有许多不同的碰撞组合。每个游戏都很独特,不同的组合可能更好地适用于不同类型的游戏。如果你在游戏中使用了物理效果,那理解不同的基本碰撞类型会很有帮助,包括它们的常用形式及如何和其他类型的对象作用。

Static Collider 静态碰撞器

These are GameObjects that do not have a Rigidbody attached, but do have a Collider attached. These objects should remain still, or move very little. These work great for your environment geometry. They will not move if a Rigidbody collides with them.

指的是没有附加刚体而附加了碰撞器的游戏对象。这类对象会保持静止或者很轻微的移动。对于环境模型十分好用,当和刚体碰撞时而不会移动。

Rigidbody Collider 刚体碰撞器

These GameObjects contain both a Rigidbody and a Collider. They are completely affected by the physics engine through scripted forces and collisions. They might collide with a GameObject that only contains a Collider. These will likely be your primary type of Collider in games that use physics.

指的是同时附加了刚体和碰撞器的游戏对象。通过脚本的力量和碰撞完全受物理引擎的影响。可以和只包含碰撞器的游戏对象碰撞。将会成为你游戏中使用物理效果的基本类型碰撞器。

Kinematic Rigidbody Collider 运动学刚体碰撞器

This GameObject contains a Collider and a Rigidbody which is marked IsKinematic. To move this GameObject, you modify its Transform Component, rather than applying forces. They're similar to Static Colliders but will work better when you want to move the Collider around frequently. There are some other specialized scenarios for using this GameObject.

指的是同时包含碰撞器和刚体,并且激活IsKinematic的一类游戏对象,要移动这类游戏对象,要修改它的Transform组件,而不是用力。它们很像静态碰撞器不过对于想要不停地到处移动的碰撞器很好用。这类游戏对象还有许多其他的独特使用情景。

This object can be used for circumstances in which you would normally want a Static Collider to send a trigger event. Since a Trigger must have a Rigidbody attached, you should add a Rigidbody, then enable IsKinematic. This will prevent your Object from moving from physics influence, and allow you to receive trigger events when you want to.

这类对象可以用于你通常希望用静态碰撞器发出触发事件的情形下。因为触发器必须要有刚体,你应该添加刚体然后启用IsKinematic。这会阻止你的对象因物理作用而移动并且允许接收你希望得到的触发事件。

Kinematic Rigidbodies can easily be turned on and off. This is great for creating ragdolls, when you normally want a character to follow an animation, then turn into a ragdoll when a collision occurs, prompted by an explosion or anything else you choose. When this happens, simply turn all your Kinematic Rigidbodies into normal Rigidbodies through scripting.

运动学刚体启用与否很容易控制。这对创建人偶十分适用,通常,当你希望一个角色依照动画活动,然后由于爆炸或者其他你选择的情况而引起的碰撞导致其变为人偶。这种情况下,很简单,只需要通过脚本将运动学刚体改为普通刚体。

If you have Rigidbodies come to rest so they are not moving for some time, they will "fall asleep". That is, they will not be calculated during the physics update since they are not going anywhere. If you move a Kinematic Rigidbody out from underneath normal Rigidbodies that are at rest on top of it, the sleeping Rigidbodies will "wake up" and be correctly calculated again in the physics update. So if you have a lot of Static Colliders that you want to move around and have different object fall on them correctly, use Kinematic Rigidbody Colliders.

如果有刚体有时候变得静止不动了,那么它们就是进入休眠了。意味着它们将不受物理变化影响,也就不会移动。如果你把一个运动学刚体从一个休眠的普通刚体下边移走,那休眠的刚体就会被唤醒并重新被物理变化时时影响。所以如果你有许多希望四处移动并有不同的对象要恰好砸到它们的静态碰撞器,那就用运动学刚体碰撞器吧。

Collision action matrix 碰撞行为矩阵

Depending on the configurations of the two colliding Objects, a number of different actions can occur. The chart below outlines what you can expect from two colliding Objects, based on the components that are attached to them. Some of the combinations only cause one of the two Objects to be affected by the collision, so keep the standard rule in mind - physics will not be applied to objects that do not have Rigidbodies attached.

基于两个碰撞对象的配置,可以产生很多不同的效果。下表概括了基于附加不同组件的两个碰撞对象所产生的效果。其中有些组合只能导致碰撞的两个对象中的一个受到影响,.所以考虑到保持标准的规则-物理效果将不会对没有附加刚体的对象生效。

Collision detection occurs and messages are sent upon collision
碰撞后有碰撞检测并有碰撞信息发出
 Static Collider
静态碰撞器
Rigidbody Collider
刚体碰撞器
Kinematic
Rigidbody Collider
运动学刚体碰撞器
Static
Trigger Collider
静态触发碰撞器
Rigidbody
Trigger Collider
刚体触发碰撞器
Kinematic Rigidbody
Trigger Collider
运动学刚体触发碰撞器
Static Collider 静态碰撞器 Y    
Rigidbody Collider 刚体碰撞器YYY   
Kinematic Rigidbody Collider
运动学刚体碰撞器
 Y    
Static Trigger Collider
静态触发碰撞器
      
Rigidbody Trigger Collider
刚体触发碰撞器
      
Kinematic Rigidbody Trigger Collider
运动学刚体触发碰撞器
      
Trigger messages are sent upon collision
碰撞后有触发信息
 Static Collider
静态碰撞器
Rigidbody Collider
刚体碰撞器
Kinematic
Rigidbody Collider
运动学刚体碰撞器
Static
Trigger Collider
静态触发碰撞器
Rigidbody
Trigger Collider
刚体触发碰撞器
Kinematic Rigidbody
Trigger Collider
运动学刚体触发碰撞器
Static Collider 静态碰撞器    YY
Rigidbody Collider 刚体碰撞器   YYY
Kinematic Rigidbody Collider
运动学刚体碰撞器
   YYY
Static Trigger Collider
静态触发碰撞器
 YY YY
Rigidbody Trigger Collider
刚体触发碰撞器
YYYYYY
Kinematic Rigidbody Trigger Collider
运动学刚体触发碰撞器
YYYYYY

Layer-Based Collision Detection 基于层的碰撞检测

In Unity 3.x we introduce something called Layer-Based Collision Detection, and is that you can now selectively tell Unity GameObjects to collide with specific layers they are attached to. For more info, you can click here

在Unity3以后版本,我们介绍了一种称为"基于层的碰撞检测"(Layer-Based Collision Detection)的东西,可以让你有选择地控制Unity对象和他们附加的特定层碰撞。点击此处获取更多信息。