Unity physics system script programming (below)

1. Modify the physical material

Unity makes a piece-by-piece treatment of the properties of the surface material of the object, and there are only 5 commonly used properties:

  • Dynamic Friction
  • Static Friction
  • Bounciness
  • Friction Combine (friction coefficient algorithm)
  • Bounce Combine (elastic coefficient algorithm)

Physical materials are used in conjunction with colliders. The collider has a Material property, which can be specified by dragging the created physical material onto the property.

Colliders have a default physical material when no physical material is specified.

Two, FixedUpdate

Physical update to ensure a stable time interval (default 0.02s, which can be modified through Edit->Project Settings->Time).

Physical systems are very time sensitive. To give an example: launch a bullet, and several objects after 0.1 seconds. If the update frequency is unstable, the bullet is not detected in time when it touches the object, and if it is 0.02 seconds later, the bullet has already passed through the object. In this way, the bullet missed the timing of the collision, resulting in completely different subsequent results.

It stands to reason that the higher the refresh rate, the better, but after considering the load and performance of the machine, it is decided that 50 frames is the most reasonable choice.

All physics processing is done in a dedicated part of the engine loop to keep the update frequency constant.

Special mechanics:

If the device is stuck for a while, can the physical update still guarantee the update frequency?

The answer is that there are ways to guarantee this indirectly. To put it simply, the time in the game world is a virtual concept, which can be controlled by humans to a certain extent. If at time T, the card is stuck for 0.06 seconds, and exactly three cycles are missed, then the next time it does not get stuck, the FixedUpdate function will make up for the three missed times, execute it 4 times in a row, and "pretend" these 4 times The call time points are T+0.02, T+0.04, T+0.06T+0.08. Through this mechanism, it can ensure that the game can guarantee "stable" physical updates regardless of whether the hardware is running stably, avoiding strange results . In contrast, Update does not have this feature.

3. Modify the angular velocity

Similar to modifying the speed of a rigid body, the code is as follows

Rigid.angularVelocity=new Vector3(0,60,0);

The data type of the angularVelocity attribute of the rigid body is Vector3, which represents the rotation speed of the x, y, and z axes, and the unit is radian/s, that is to say, 3.14 represents half a circle per second.

Due to Angular Drag, the rotation will stop slowly even if it does not touch other objects.

4. Centroid

Stitch a simple model and use the physics system to adjust its center of gravity to make just a tumbler. code show as below

Rigidbody rigid;

rigid=GetComponent<Rigidbody>();

//Set centerOfMass to specify the center of gravity

rigid.centerOfMass=new Vector3(0,-1,0);

The center of gravity of the game object is not limited by the real world, not only can be set at any position of the object, but also beyond the range of the object itself.

When a force is applied to an object, the effect varies depending on where the force is located. Strictly speaking, for an object without any force (in Unity, it is a rigid body that removes neutrality and does not contact other objects), if the direction of the hand passes through the center of mass of the object, the object will not gain a higher speed . If the direction of the force misses the center of mass, then the object will have a tendency to spin. The greater the distance from the center of mass to the line of force, the stronger the tendency to rotate

5. More ways to apply force

The position of the force is important, but the AddForce mentioned above does not have a position parameter. It can be guessed that when the AddForce function applies force, it is applied from the center of mass of the object.

If you want to simulate more complex situations, you can use the following functions:

void AddForceAtPosition(Vector3 force,Vector3 position);

void AddForceAtPosition(Vector3 force,Vector3 position,ForceMode mode);

The second parameter is the position where the force is applied, expressed in world coordinates, so the coordinates may need to be transformed when using it.

The third parameter is the force mode, which is an enumeration type, defined as follows:

public enum ForceMode

{

//The default method is to apply force continuously

Force=0,

//Set to instantaneous force, suitable for expressing fast and violent force, such as explosion

//The duration of the force is different

Impulse=1,

//Change the speed of the rigid body instantaneously, regardless of the mass

VelocityChange=2,

// Change acceleration directly, regardless of mass

Acceleration=5

That is to say, when applying force, the meaning of applying force can be changed by changing the nibble mode. The first two are more commonly used, and the third can be replaced by the velocity attribute that directly modifies the velocity of the rigid body.

6. Rigid Body Constraints

In Rigidbody, there is a Constraints that can lock the movement and rotation of the xyz axis. Locking some degrees of freedom as needed can make the behavior of the rigid body more controllable.

It is also possible to control freezing with a script:

rigid.constraints=RigidbodyConstraints.FreezeAll;//freeze all movement and rotation

rigid.constraints=RigidbodyConstraints.FreezePositionX;//Freeze the displacement of the x-axis and cancel all other constraints

rigid.constraints=RigidbodyConstraints.FreezeRotation;//Freeze all rotations, cancel displacement constraints

rigid.constraints=RigidbodyConstraints.FreezeRotationX|RigidbodyConstraints.FreezeRotationZ|RigidbodyConstraints.FreezePositionY;//Freeze x and z axis rotation, freeze y axis displacement

Guess you like

Origin blog.csdn.net/m0_63024355/article/details/130585344