Unity Best Practices-Physical computing
References: https://unity3d.com/cn/learn/tutorials/topics/physics/physics-best-practices hierarchy (layer) and hierarchical collision matrix (layer Collision Matrix)
All newly created gameobject defaults to the default layer, and all objects in that layer can collide with each other, which will compare consumption performance.
Different layers should be set for different types of gameobject to set which gameobject can collide.
The newly added layer defaults to a collision with a layer that already exists, eliminating unnecessary collision relationships in the collision matrix to reduce collision monitoring and collision events.
The collision matrix interface is located in the edit-project settings-physics:layer collision Matrix. Ray (raycasts)
The
Ray compares consumption performance, and its performance overhead is closely related to the length of the ray and the type of Collider (Collider) in the scene.
Here are some tips for optimizing ray performance: Minimize the number of rays. The length of the ray is sufficient. The longer the ray, the more objects the engine will detect. Do not use rays in fixedupdate () and try not to use them in Update (). Note the type of collider detected by the Ray, and the Meshcollider-ray processing performance overhead is very high.
for objects with complex meshes, a better solution is to create multiple sub-objects with the underlying collider, and then adjust the shape of the sub-object collider to resemble the parent object. If you do need to use Meshcollider, try to set it to convex. Clarify which layers the ray needs to collide with, and use hierarchical masks as much as possible in the Ray method. The Layermask parameter in the
Ray method is used to set the hierarchy matte, which is a bitmask rather than a layer ID. To set the ray to collide with only the 10th (Layer ID 10) object, the Layermask parameter should be 1<<10. To set the ray not to collide with an object in the 10th layer, the Layermask parameter should be ~ (1<<10), and the parentheses cannot be omitted. The ID of the layer can be obtained through the Layermask.nametolayer ("LayerName") method. Example 1:physics.raycast (Ray, out hit, 1 << 10). Example 2:physics.raycast (Ray, out hit, ~ (1 << 10)). rigid Body (rigidbody)
A rigid body is a component necessary for physical interaction between objects, and even if you only use a collider as a trigger (Tigger), you should add a rigid component to the object, which ensures that the Ontrigger event works correctly.
A collider object without a rigid body is considered by the engine to be a static collider (Collider). To avoid moving a static collider object, this is a costly operation because it causes the engine to recalculate the physical data for the entire scene.
the CPU profile Bar of the profile window outputs a warning (warning) when moving a static collider. Fixed time step (fixed timestep)
Adjusting the Fixed Timestep in the time manager directly affects the update frequency of the Fixedupdate () and the physical calculations, and seeks to balance the physical computational precision and CPU consumption by changing the value. The time manager interface is located at edit-project settings-time:time Manager.