Physics
Manual     Reference     Scripting   
Unity Manual > User Guide > Creating Gameplay > Physics

このドキュメントは有志により翻訳されたもので、オフィシャルではありません。オリジナルのページはこちら
This document is unofficially translated by users.Please see the original document here.

翻訳に関する修正など、ご連絡はこちらまで。
Please send e-mail to here, when you have any question about the translation.

Physics

Unity has the next-generation Ageia PhysX physics engine built-in. This allows for unique emergent behaviour and is generally very cool.

Basics

To put an object under physics control, simply add a Rigidbody to it. When you do this, the object will be affected by gravity, and can collide with other objects in the world.

Rigidbodies

You use Rigidbodies for things that the player can push around, eg. crates or loose objects. You can also add Joints to rigidbodies to make the behavior more complex. For example, you could make a physical door or a crane with a swinging chain.

You also use Rigidbodies to bring vehicles to life, for example you can make cars using a Rigidbody, 4 Wheel Colliders and a script applying wheel forces based on the user's Input.

You can make airplanes by applying forces to the Rigidbody from a script. Or you can create special vehicles or robots by adding various Joints and applying forces via scripting.

Rigidbodies are most often used in combination with primitive colliders.

Tips:

Kinematic Rigidbodies

Kinematic Rigidbodies are not affected by forces, gravity or collisions. They are driven explicitly by setting the position and rotation of the transform or animating them, yet they can interact with other non-Kinematic Rigidbodies.

Kinematic Rigidbodies are used for three purposes:

  1. Sometimes you want an object to be under physics control but in another situation to be controlled explicitly from a script or animation. For example you could make an animated character whose bones have Rigidbodies attached that are connected with joints for use as a Ragdoll. Most of the time the character is under animation control, thus you make the Rigidbody Kinematic. But when he gets hit you want him to turn into a Ragdoll and be affected by physics. To accomplish this, you simply disable the isKinematic property.
  2. Kinematic Rigidbodies play better with other Rigidbodies. For example if you have an animated platform and you want to place some Rigidbody boxes on top, you should make the platform a Kinematic Rigidbody instead of just a Collider without a Rigidbody.
  3. You might want to have a Kinematic Rigidbody that is animated and have a real Rigidbody follow it using one of the available Joints.

Static Colliders

Static Colliders are used for level geometry which does not move around much. You add a Mesh Collider to your already existing graphical meshes (even better use the Import Settings Generate Colliders check box). You can still move around Static Colliders, but if you move them around a lot, you might want to add a Kinematic Rigidbody. There are two reasons why you want to make a Static Collider into a Kinematic Rigidbody instead:

  1. Kinematic Rigidbodies wake up other Rigidbodies when they collide with them.
  2. Kinematic Rigidbodies apply friction to Rigidbodies placed on top of them

Character Controllers

You use Character Controllers if you want to make a humanoid character. This could be the main character in a third person platformer, FPS shooter or any enemy characters.

These Controllers don't follow the rules of physics since it will not feel right (in Doom you run 90 miles per hour, come to halt in one frame and turn on a dime). Instead, a Character Controller performs collision detection to make sure your characters can slide along walls, walk up and down stairs, etc.

Character Controllers are not affected by forces but they can push Rigidbodies by applying forces to them from a script. Usually, all humanoid characters are implemented using Character Controllers.

Character Controllers are inherently unphysical, thus if you want to apply real physics - Swing on ropes, get pushed by big rocks - to your character you have to use a Rigidbody, this will let you use joints and forces on your character. But be aware that tuning a Rigidbody to feel right for a character is hard due to the unphysical way in which game characters are expected to behave.

If you parent a Character Controller with a Rigidbody you will get a "Joint" like behavior.

Constant Force

Constant Force is a quick utility for adding constant forces to a Rigidbody. This works great for one shot objects like rockets, if you don't want it to start with a large velocity but instead accelerate.


A rocket propelled forward by a Constant Force

Properties

ForceThe vector of a force to be applied in world space.
Relative ForceThe vector of a force to be applied in the object's local space.
TorqueThe vector of a torque, applied in world space. The object will begin spinning around this vector. The longer the vector is, the faster the rotation.
Relative TorqueThe vector of a torque, applied in local space. The object will begin spinning around this vector. The longer the vector is, the faster the rotation.

Details

To make a rocket that accelerates forward set the Relative Force to be along the positive z-axis. Then use the Rigidbody's Drag property to make it not exceed some maximum velocity (the higher the drag the lower the maximum velocity will be). In the Rigidbody, also make sure to turn off gravity so that the rocket will always stay on its path.

Hints

Sphere Collider

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


A pile of Sphere Colliders

Properties

MaterialReference to the Physic Material that determines how this Collider interacts with others.
Is TriggerIf enabled, this Collider is used for triggering events, and is ignored by the physics engine.
RadiusThe size of the Collider.
CenterThe 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

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.

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 Ageia's physX engine.

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!

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.

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

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.

Hints

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.

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.

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.

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 ColliderRigidbody ColliderKinematic
Rigidbody Collider
Static
Trigger Collider
Rigidbody
Trigger Collider
Kinematic Rigidbody
Trigger Collider
Static Collider Y    
Rigidbody ColliderYYY   
Kinematic Rigidbody Collider Y    
Static Trigger Collider      
Rigidbody Trigger Collider      
Kinematic Rigidbody Trigger Collider      
Trigger messages are sent upon collision
 Static ColliderRigidbody ColliderKinematic
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 ColliderYYYYYY
Kinematic Rigidbody Trigger ColliderYYYYYY

Box Collider

The Box Collider is a basic cube-shaped collision primitive.


A pile of Box Colliders

Properties

MaterialReference to the Physic Material that determines how this Collider interacts with others.
Is TriggerIf enabled, this Collider is used for triggering events, and is ignored by the physics engine.
SizeThe size of the Collider in the X, Y, Z directions.
CenterThe position of the Collider in the object's local space.

Details

The Box Collider can be resized into different shapes of rectangular prisms. It works great for doors, walls, platforms, etc. It is also effective as a human torso in a ragdoll or as a car hull in a vehicle. Of course, it works perfectly for just boxes and crates as well!


A standard Box 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.

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 Ageia's physX engine.

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!

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.

Mesh Collider

The Mesh Collider takes a Mesh Asset and builds its Collider based on that mesh. It is far more accurate for collision detection than using primitives for complicated meshes. Mesh Colliders that are marked as Convex can collide with other Mesh Colliders.


A Mesh Collider used on level geometry

Properties

MaterialReference to the Physic Material that determines how this Collider interacts with others.
Is TriggerIf enabled, this Collider is used for triggering events, and is ignored by the physics engine.
MeshReference to the Mesh to use for collisions.
Smooth Sphere CollisionsWhen this is enabled, collision mesh normals are smoothed. You should enable this on smooth surfaces eg. rolling terrain without hard edges to make sphere rolling smoother.
ConvexIf enabled, this Mesh Collider will collide with other Mesh Colliders. Convex Mesh Colliders are limited to 255 triangles.

Details

The Mesh Collider builds its collision representation from the Mesh attached to the GameObject, and reads the properties of the attached Transform to set its position and scale correctly.

Collision meshes use backface culling. If an object collides with a mesh that will be backface culled graphically it will also not collide with it physically.

There are some limitations when using the Mesh Collider. Usually, two Mesh Colliders cannot collide with each other. All Mesh Colliders can collide with any primitive Collider. If your mesh is marked as Convex, then it can collide with other Mesh Colliders.

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.

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 Ageia's physX engine.

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!

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.

Hints

Physic Material

The Physic Material is used to adjust friction and bouncing effects of colliding objects.

To create a Physic Material select Assets->Create->Physic Material from the menu bar. Then drag the Physic Material from the Project View onto a Collider in the scene.


The Physic Material Inspector

Properties

Dynamic FrictionThe friction used when already moving. Usually a value from 0 to 1. A value of zero feels like ice, a value of 1 will make it come to rest very quickly unless a lot of force or gravity pushes the object.
Static FrictionThe friction used when an object is laying still on a surface. Usually a value from 0 to 1. A value of zero feels like ice, a value of 1 will make it very hard to get the object moving.
BouncynessHow bouncy is the surface? A value of 0 will not bounce. A value of 1 will bounce without any loss of energy.
Friction Combine ModeHow the friction of two colliding objects is combined.
AverageThe two friction values are averaged.
MinThe smallest of the two values is used.
MaxThe largest of the two values is used.
MultiplyThe friction values are multiplied with each other.
Bounce CombineHow the bouncyness of two colliding objects is combined. It has the same modes as Friction Combine Mode
Friction Direction 2The direction of anisotropy. Anisotropic friction is enabled if this direction is not zero. Dynamic Friction 2 and Static Friction 2 will be applied along Friction Direction 2.
Dynamic Friction 2If anisotropic friction is enabled, DynamicFriction2 will be applied along Friction Direction 2.
Static Friction 2If anisotropic friction is enabled, StaticFriction2 will be applied along Friction Direction 2.

Details

Friction is the quantity which prevents surfaces from sliding off each other. This value is critical when trying to stack objects. Friction comes in two forms, dynamic and static. Static friction is used when the object is lying still. It will prevent the object from starting to move. If a large enough force is applied to the object it will start moving. At this point Dynamic Friction will come into play. Dynamic Friction will now attempt to slow down the object while in contact with another.

Hints

Hinge Joint

The Hinge Joint groups together two Rigidbodies, constraining them to move like they are connected by a hinge. It is perfect for doors, but can also be used to model chains, pendulums, etc.


The Hinge Joint Inspector

Properties

Connected BodyOptional reference to the Rigidbody that the joint is dependent upon. If not set, the joint connects to the world.
AnchorThe position of the axis around which the body swings. The position is defined in local space.
AxisThe direction of the axis around which the body swings. The direction is defined in local space.
Use SpringSpring makes the Rigidbody reach for a specific angle compared to its connected body.
SpringProperties of the Spring that are used if Use Spring is enabled.
SpringThe force the object asserts to move into the position.
DamperThe higher this value, the more the object will slow down.
Target PositionTarget angle of the spring. The spring pulls towards this angle measured in degrees.
Use MotorThe motor makes the object spin around.
MotorProperties of the Motor that are used if Use Motor is enabled.
Target VelocityThe speed the object tries to attain.
ForceThe force applied in order to attain the speed.
Free SpinIf enabled, the motor is never used to brake the spinning, only accelerate it.
Use LimitsIf enabled, the angle of the hinge will be restricted within the Min & Max values.
Limits Properties of the Limits that are used if Use Limits is enabled.
MinThe lowest angle the rotation can go.
MaxThe highest angle the rotation can go.
Min BounceHow much the object bounces when it hits the minimum stop.
Max BounceHow much the object bounces when it hits the maximum stop.
Break ForceThe force that needs to be applied for this joint to break.
Break TorqueThe torque that needs to be applied for this joint to break.

Details

A single Hinge Joint should be applied to a GameObject. The hinge will rotate at the point specified by the Anchor property, moving around the specified Axis property. You do not need to assign a GameObject to the joint's Connected Body property. You should only assign a GameObject to the Connected Body property if you want the joint's Transform to be dependent on the attached object's Transform.

Think about how the hinge of a door works. The Axis in this case is up, positive along the Y axis. The Anchor is placed somewhere at the intersection between door and wall. You would not need to assign the wall to the Connected Body, because the joint will be connected to the world by default.

Now think about a doggy door hinge. The doggy door's Axis would be sideways, positive along the relative X axis. The main door should be assigned as the Connected Body, so the doggy door's hinge is dependent on the main door's Rigidbody.

Chains

Multiple Hinge Joints can also be strung together to create a chain. Add a joint to each link in the chain, and attach the next link as the Connected Body.

Hints