The physical engine is the real physical effect simulated in the game. For example, two objects are collided and the objects are freely lost. In unity, NVIDIA physx is used to render realistic game images.
Rigid Body
A rigid body is a very, very important component. By default, new objects do not have physical effects, while rigid body components can add some common physical properties to objects. For example, quality, friction, and collision. If a rigid body component is added to an object, it senses all the physical effects in the physical engine.
Easy to use
First wear a game object, then select the created game object in the hierarchy view, and then select component -- physics -- Rigidbody in the Unity navigation menu bar. The following three cubes are created and placed on the lingkong ground. Add a rigid body to one of the cubes. After running the tool, the red cube senses the physical effect and falls down. The other two remain in the same position.
Attributes of a rigid body:
Mass: mass. The larger the value, the faster the object falls. Try not to exceed 10. Otherwise, it will appear untrue.
Drag: resistance. The greater the value, the faster the object slows down.
Angular drag: Angle resistance. The greater the value, the faster the rotation speed slows down.
Use gravity: whether to use gravity.
Is kinematic: whether it is physically affected.
Interpolate: sets the image difference.
Collision Detection: collision monitoring.
Constrains: Freeze, stop the effect of an Axial induction physical Engine
Freeze position: freeze the X axis, Y axis, Z axis, and direction.
Freeze rotation: freeze the X axis, Y axis, Z axis, and rotation.
Physical Manager
In the physical manager, you can set all the physical effects of the entire project. For example, gravity, elastic, and speed. In the menu bar, select edit -- Project setting -- physics to open physics manager. For example:
Gravity: gravity. By default, the downward gravity of an object on the Y axis is 9.8 n. The force direction of the three axes can be modified.
Default material: the default physical material.
Bounce threshold: rebound value.
Sleep velocity: Sleep speed. When the sleep speed is lower, it remains static.
Sleep angular velocity: Sleep angular velocity. When the sleep speed is lower than the sleep speed, it does not rotate itself.
Max angular velocity: maximum angular velocity.
Min penetration for penalty: used for minimum penetrating force after collision between an object and an object.
Solver iteration count: number of iterations. The default value is 7.
Raycasts hit triggers: whether to enable hit trigger.
Layer collision matrix: layer collision matrix.
Force
A rigid body can be forced. For example, if a force is given to the X axis, the rigid body moves forward along the X axis. In unity, there are two ways of force: normal force, you need to set the direction and size of the force. Target Position force. You need to set the target point position. This object will apply force to this target position.
The following is an example. A total of two ball objects are placed. After clicking the normal force, the ball is kicked. Click the position force, and the ball will be applied with a force toward the target.
Code:
Using unityengine; using system. collections; public class script_06_02: monobehaviour {// The object gameobject addfrceobj = NULL that applies the normal force; // The object gameobject addposobj = NULL that applies the target position force; // target object gameobject cubeobj = NULL; // use this for initializationvoid start () {// obtain the object addfrceobj = gameobject that applies normal force. find ("sphere0"); // obtain the object addposobj = gameobject that applies the target position force. find ("sphere1"); // obtain the target object cubeobj = gameobject. find ("cube");} void ongui () {If (guilayout. Button ("normal force? ", Guilayout. height (50) {// apply a force. The intensity of the X axis is 1000, and the intensity of the Y axis is 1000addfrceobj. rigidbody. addforce (1000, 0, 1000);} If (guilayout. button ("location force", guilayout. height (50) {// apply a single position force. The object moves toward this position, and the Force mode is the impact. Vector3 force = cubeobj. Transform. Position-addposobj. Transform. position; addposobj. Rigidbody. addforceatposition (force, addposobj. Transform. Position, forcemode. Impulse );}}}
Run: