Collision and sleep
As mentioned in the previous note, when the game object is given a rigid body component, this component may be subject to a collision. Once a rigid body starts to move, the system method will monitor the collision status of the rigid body. Generally, there are three types of Rigid Body Collisions: Entry collision, collision in progress, and collision end. Sleep can be understood as turning game objects into exquisite states. Assuming that a game object is in sleep state, the object immediately becomes exquisite and does not move any more.
Collision
First, we will learn several important methods of collision:
Oncollisionenter (): it is called immediately when the rigid body is in contact.
Oncollisionstay (): This method is called for each frame during the collision process until the impact ends.
Oncollisionexit (): called when the collision stops.
Code (the following code is only part of the entire example. If you need to recreate the project, I will upload all the code after writing the entire unity entry ):
Using unityengine; using system. collections; public class collisiontest: monobehaviour {// collision display information string show = NULL; void start () {// The default display content show = "no collision ";} // enter the collision void oncollisionenter (collision) {Show = "Enter the collision, collision name:" + collision. gameobject. name;} // void oncollisionstay (collision) {Show = "collision, collision name:" + collision. gameobject. name;} // void oncollisionexit (collision) {Show = "collision ended, collision name:" + collision. gameobject. name; // after the collision, let the object sleep collision. gameobject. rigidbody. sleep ();} void ongui () {// displays the collision information to the GUI. label (New rect (,), show );}}
Collision Server
If a game object requires an inductive collision, it must be added to the collision generator. By default, when a game object is created, the collision generator component is automatically added, and the collision generator component determines the mode of model collision. Unity provides five collision servers, including box collider, sphere collider, and capsule collider ), mesh collider and wheel collider ). Box collider is applicable to the collision with cube objects. Sphere collider is applicable to the collision between sphere objects and capsule collider) the mesh collider is suitable for defining the mesh decision of the model itself. The wheel collider is suitable for collision between the wheel and the ground or other objects.
Add a collision Generator
In the menu bar, select component -- physics and select a type.
Physical Material
The physical material can be used to set the surface material of an object. Different Surface materials can affect the physical effect after collision. The physical material can be added to any collision tool. First, you must import the physical material resources and select the standard package provided by the import system. Import -- physic materials. 5 frequently used materials are provided in the standard package: Bouncy (elastic), ice (ICE), metal (metal), rubber (rubber), wood (wood)
, Added an elastic material to the ball. When the ball falls down, it will play back when it comes to the ground.
In addition to the five physical materials in the Unity standard resource package, you can also create physical materials on your own. Click Create -- physic material in the project view. For example:
Dynamic Friction: Dynamic Friction. The value ranges from 0 to 1. The value 0 is the minimum and the value 1 is the maximum.
Static Friction: static friction. The value ranges from 0 to 1. The value 0 is the minimum and the value 1 is the maximum.
Bounciness: collision rebound coefficient. The value ranges from 0 to 1, and the minimum value is 0. The maximum value is 1.
Friction combine: The friction mode after normal collision.
Bounce combine: The friction mode after rebound and collision.
Friction direction 2: Friction direction, X axis, Y axis, and Z axis.
Dynamic Friction 2: Dynamic Friction Coefficient. The friction direction is set according to friction direction 2.
Static Friction 2: static friction coefficient. The friction direction is set according to friction direction 2.
[Unity 3D] Study Notes: Physical engine-collision and sleep