- Home /
Difference between character controller and Rigidbody + Collider
Hello everyone , I was watching a tutorial that was talking about Physics and Collisions . We had a player that is a cylinder and we moved it with the following script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float MoveSpeed;
public float RotateSpeed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float V = Input.GetAxis("Vertical");
float H = Input.GetAxis("Horizontal");
transform.rotation *= Quaternion.Euler(0f, H * Time.deltaTime * RotateSpeed ,0f);
transform.position += transform.forward * MoveSpeed * Time.deltaTime * V;
}
}
The problem was that the character was going through walls and objects . The person explaining then added a character controller and used the character controller with velocity parameter instead of transform. position then have the character had gravity and collided with objects in the scene. I am really used to add a rigid body and a collider to a player whenever I need to achieve those effects. So I was wondering what the difference between those two methods were. Can anyone help explain ? Thanks in advance
Use rigidbody.velocity not transform. if you want use transform, then you need additional checks with raycasts or something like that.
Answer by Eno-Khaon · May 18, 2017 at 10:32 PM
Well, a Rigidbody, at a simple and fundamental level, is used to have easily-manipulatable objects in your environment. This may mean things like having balls roll around and boxes tumble.
A CharacterController, by contrast, is intended to act as an easy-to-use integration of physical interaction for a player-driven character. The logical basis is to give the player explicit control over the character's movement.
Essentially, a CharacterController lets you have a Kinematic Rigidbody that can still be stopped by other physics objects. By having all control (generally) be given exclusively to the player, you won't see any unpredictable results which might occur by having the full range of physics interactions provided by a Rigidbody.
That's not to say that a Rigidbody is a bad choice. It's really a matter of personal taste and design intent. For example, the Roll-a-ball Tutorial is based around a game design as it describes: You roll a ball around, and let it bounce off of walls as it will. Your character is the ball, but you gladly invite arbitrary movement outside the direct input the player provides.
To give an example of personal preference as a factor, I generally prefer to use a Rigidbody and define a strict set of movement rules for it. The default CharacterController inherently supports the Y-axis as its "vertical" axis, but I take particular interest in having control schemes which don't rely on a fixed direction for gravity, and apply force to individual objects myself to function as their "personal" gravity. Additionally, there are some situations where I may want a heavy environment object to push the player in a non-predetermined manner, which can be easily applied when using a Rigidbody-based character.
In short, both Rigidbody and CharacterController can fundamentally serve the same purpose. The CharacterController is designed to make it easy to hook up and control an object, but it may or may not be exactly what you're looking for in a control scheme.
So a character controller is like having something ready while a rigid body needs more work , thus making the rigidbody offer more customization ?
That's effectively the gist of it. That's not to say that a CharacterController *can't* be customized, but depending on what you're looking to do, either one can fill its role in specific ways without much additional effort.