转篇好文--关于碰撞检测的

原文:http://answers.unity3d.com/questions/149/guidelines-for-using-rigidbody-collider-charactercontrollerscript-etc

Guidelines for using rigidbody, collider, CharacterControllerScript, etc?Ask:
Understanding the physics engine and all the options for controlling game objects can be quite daunting for a Unity newbie. How do I know when to use a rigidbody (kinematic or non-kinematic), a collider, the CharacterControllerScript, or something else?
What are the basic guidelines for what to use, and how/when to use them?
Answer:
OverviewHere are some general rules-of-thumb.

  • Static scenery
    • Collider, No Rigidbody
  • Movable scenery/objects
  • Characters
    • CharacterController (includes a Capsule Collider) + Custom control script
    • OR Collider + Custom control script (can cause issues if your character interacts with rigidbodies)
    • OR Collider + Rigidbody (IsKinematic=true) + Custom control script
    • OR Collider + Rigidbody (IsKinematic=false) + One or more Configurable Joints (which can be used to constrain the rigidbody's motion or rotation, if necessary) + Custom control script

MovementWhen using CharacterController, movement is accomplished by calling the functions exposed by CharacterController. Interaction with other objects (platforms, crates, etc.) is accomplished by implementing the collision events exposed by CharacterController and applying forces to the objects the character collides with, or moving the character in response to the movement of the other objects.
When using a kinematic rigidbody, movement is accomplished by directly modifying your game object's Transform, or by calling rigidbody.MovePosition. (Note: the movement of kinematic rigidbodies should be done in FixedUpdate.)
When using a non-kinematic rigidbody, movement is typically accomplished by calling rigidbody.AddForce and rigidbody.AddTorque, or by setting the rigidbody.velocity and rigidbody.angularVelocity directly.
ResourcesA good overview of the physics engine, rigidbodies, and colliders can be found in the Physics section of the Unity manual.
DiamondTearz has also compiled a good list of Unity physics tutorials/resources.
About CharacterControllerAccording to the docs, the CharacterController component "is mainly used for third-person or first-person player control that does not make use of Rigidbody physics." In other words, the CharacterController is a handy component you can apply to a game object if you don't want it to be controlled by the physics engine, but you don't want to have to write your own custom controller code from scratch.
Why wouldn't you want your character controlled by the physics engine? Well, oftentimes a character's movement is not physically realistic. A character might run like a rocket-powered cheetah, change directions mid-air, etc.
CharacterController allows you to create this kind of unrealistic movement, but handles many of the basics for you -- things like walking up steps and keeping your character from walking through walls.
Why wouldn't you want to use CharacterController? If your character isn't a human-like entity walking on the ground, it may not be a good fit.
More info on CharacterController can be found here.

你可能感兴趣的:(转)