Adding a Rigidbody
component to an object will put its motion under the control of Unity’s physics engine. Even without adding any code, a Rigidbody
object will be pulled downward by gravity and will react to collisions with incoming objects if the right Collider component is also present.
The Rigidbody also has a scripting API that lets you apply forces to the object and control it in a physically realistic way. For example, a car’s behavior can be specified in terms of the forces applied by the wheels. Given this information, the physics engine can handle most other aspects of the car’s motion, so it will accelerate realistically and respond correctly to collisions.
In a script, the FixedUpdate
function is recommended as the place to apply forces and change Rigidbody
settings (as opposed to Update
, which is used for most other frame update tasks). The reason for this is that physics updates are carried out in measured time steps that don’t coincide with the frame update. FixedUpdate
is called immediately before each physics update and so any changes made there will be processed directly.
Reference: https://docs.unity3d.com/ScriptReference/Rigidbody.html
A CharacterController
allows you to easily do movement constrained by collisions without having to deal with rigibody.
A CharacterController is not affected by forces and will only move when you call the Move function. It will carry out the movement but be constrained by collisions.
Reference: https://docs.unity3d.com/ScriptReference/CharacterController.html
Move use position
Didn’t react to collision even have Rigidbody component.
public class PositionMove : MonoBehaviour
{
public float speed;
void Update()
{
float deltaX = Input.GetAxis( "Horizontal" ) * speed;
float deltaZ = Input.GetAxis( "Vertical" ) * speed;
Vector3 movement = new Vector3( deltaX, 0, deltaZ );
// Returns a copy of vector with its magnitude clamped to maxLength
movement = Vector3.ClampMagnitude( movement, speed );
movement *= Time.deltaTime;
transform.position += movement;
}
}
Move use Translate
Didn’t react to collision without Rigidbody component.
React to collision with Rigidbody, simulate physics.
public class TranslateMove : MonoBehaviour
{
public float speed;
void Update()
{
float deltaX = Input.GetAxis( "Horizontal" ) * speed;
float deltaZ = Input.GetAxis( "Vertical" ) * speed;
Vector3 movement = new Vector3( deltaX, 0, deltaZ );
// Returns a copy of vector with its magnitude clamped to maxLength
movement = Vector3.ClampMagnitude( movement, speed );
movement *= Time.deltaTime;
transform.Translate( movement );
}
}
Move use Rigidbody
React to collision, simulate physics.
[RequireComponent( typeof( Rigidbody ) )]
public class RigidbodyMove : MonoBehaviour
{
Rigidbody rb;
// set the value great 100
public float speed;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float deltaX = Input.GetAxis( "Horizontal" ) * speed;
float deltaZ = Input.GetAxis( "Vertical" ) * speed;
Vector3 movement = new Vector3( deltaX, 0, deltaZ );
// Returns a copy of vector with its magnitude clamped to maxLength
movement = Vector3.ClampMagnitude( movement, speed );
movement *= Time.deltaTime;
// Transforms direction from local space to world space.
movement = transform.TransformDirection( movement );
rb.AddForce( movement );
}
}
Move use CharacterController
React to collision, didn’t simulate physics.
[RequireComponent( typeof( CharacterController) )]
public class CharacterMove : MonoBehaviour
{
CharacterController cc;
// set the value great 100
public float speed;
// Use this for initialization
void Start()
{
cc = GetComponent<CharacterController>();
}
void FixedUpdate()
{
float deltaX = Input.GetAxis( "Horizontal" ) * speed;
float deltaZ = Input.GetAxis( "Vertical" ) * speed;
Vector3 movement = new Vector3( deltaX, 0, deltaZ );
// Returns a copy of vector with its magnitude clamped to maxLength
movement = Vector3.ClampMagnitude( movement, speed );
movement *= Time.deltaTime;
// Transforms direction from local space to world space.
movement = transform.TransformDirection( movement );
cc.Move( movement );
}
}