8 min read · Oct 3, 2023
--
In my last article, I have talked about “How to Enable and Disable any GameObject in Unity” and I have come back with another article for Unity developers.
In this article, we will discuss about both features of Unity. Both features have their own uses and in case of character or any object that could be moved or have a player features with keyboard buttons, developer can selected any of these two features for his operations. For developers especially beginners face issues to select between these components.
Let us talk about both features –
One of most commonly used features of Unity 3D is character controller. We can use character controller for game’s player development and other applications moving character development. Character controller does not have physics features but we have other methods, which we can use to apply physics properties to the player/character. This feature commonly used for player/character movement in the environment with help of keyboard buttons. By default in keyboard, control buttons are A, W, D, S (Left, Forward, Right and Backward). We can also modify the controls according to our condition.
When we add character controller component to any character then it creates capsule shaped collider and adds it around the player. Player can increase or decrease height and width of the space. If we want to apply movements to GameObject then we have to call Move Function from script. Then it will start moving character but there will be restrictions by collisions. i.e. — “If there is wall with box collider added as collider and player have character controller component then player won’t be able to perform “Quantum Tunneling” or we can say “Intangibility”. The walls will prevent us to go other side of wall.”
Properties of character controller –
Slope Limit : Slope limit property is the way to control slope limit in degrees. When we use script, we call it as “.slopeLimit” method after component variable. Here we write how many degrees angle it can climb-up. Same changes we can also do without script. Use of this property is — suppose we have stairs or hill in our environment and we want to climb but if our slope limit is low then it will not move forward because its limit is restricted to given value. It limits the collider to climb slopes less than degrees we have given. To make changes without script follow path for Slope Limit in Unity -
Hierarchy tab >> Character/Player object >> Inspector tab >> Character Controller >> Slope Limit
Step Offset: Step offset property helps user to control how much of height of obstacle/stair any character can climb. The height of obstacle should not be greater than step offset value because it will not climb the stairs. Always keep the value lesser than game object.
Skin Width: Skin width is helpful for the controller to prevent from being stuck. We should keep value greater 0.01.
Min Move Distance: This properties value is must to change because it maintains the minimum distance between target and controller. It should not be less than actual distance between both because controller will move from its position.
Center, Radius and Height: These three properties define the values of the wireframe, which you can see after clicking on character controller component. With these properties, we can set values based on our characters properties. The wireframe stays inside the character.
void Update()
{
playerMovement = new Vector3(Input.GetAxis("Horizontal"), 0f , Input.GetAxis("Vertical"));
cameraMovement = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); MovePlayer();
}
private void MovePlayer()
{
Vector3 playerPosition = transform.TransformDirection(playerMovement);
if(characterOperate.isGrounded)
{
velocity.y = -1f;
if(Input.GetKey(KeyCode.Space))
{
velocity.y = jumpForce;
}
}
else
{
velocity.y -= gravity * -2f * Time.deltaTime;
}
characterOperate.Move(playerPosition * speed * Time.deltaTime);
characterOperate.Move(velocity * Time.deltaTime);
}
Rigidbody is one of the most important component and feature of Unity. If you want to have real world experience in 3D then Rigidbody is necessary component for the project. Rigidbody is responsible for real world physics in Unity. We can use Rigidbody in 2D and 3D. If we are using in 2D then after Rigidbody add 2D (Rigidbody 2D). In Rigidbody, we can modify the gravity, mass of objects, kinematic values and many other options. Rigidbody calculates and executes realistic interaction between groups of objects. Rigidbody works with Collision component.
Rigidbody 2D Component –
In Rigidbody 2D component, properties are somewhat different from 3D component. In 2D component, we use sprites as objects. In 2D component, we will get many properties and some of them are –
Body Type — Body type have three options — Dynamic, which causes Rigidbody to react to gravity and applied forces and should move when we apply forces. Another one is Kinematic; this option stops rigidbody to react to gravity and applied forces. We can move it by setting Rigidbody2D.velocity or Rigidbody2D.angularVelocity or by setting Transform. Static is also same as Kinematic, which stops Rigidbody to react and it should never reposition explicitly and it is designed to never move also.
Materiel — This property describes friction and bounciness of collisions means we can adjust the friction and bounciness that occurs between 2D physics objects when they collide.
Mass — Mass affects momentum, which is significant during collisions. The higher the mass the more it would be difficult to move it. For example, if we take a bowling ball with high mass. It would be difficult to pick up and even more difficult to roll it. If we take beach ball with very less mass, with small movement touching will move it much further compared to bowling ball.
We can use other 2D components with Rigidbody for better effects and more functions. Some of the components are Collider 2D components (Circle collider 2D, Box Collider 2D, Edge Collider 2D, Capsule Collider 2D, Composite Collider 2D), 2D Effectors, 2D Joints.
Rigidbody 3D Component –
Unity have two separate physics system — 2D and 3D. The principles, components, naming and C# codes are similar but not identical. In 2D, the GameObject uses Rigidbody 2D component and in 3D, the GameObject used Rigidbody component. Rigidbody component enables physical behavior for a GameObject. When we want to use Rigidbody component for a GameObject we also need to add collider with it and it depends on GameObject and situation, which one we should use. There are also multiple colliders for 3D objects like — Sphere Collider, Box Collider, Capsule Collider and Mesh Collider, which comes default with GameObject if we use GameObject from asset store.
One more thing to be I want also to be clear about is –
> In 2D, Rigidbody references the PhysicsMaterial2D (With s).
> In 3D, Rigidbody references the PhysicMaterial (Without s).
Some more options different in 3D Rigidbody component are –
Use Gravity — Use Gravity property in Rigidbody controls whether gravity affects the GameObject or not. If we set it to false, then Rigidbody will behave like if it is in outer space (without a constant force applied to it in some direction). When we set, useGravity to true then Rigidbody will experience force of -9.81 world units per second means as negative Y-values in Unity’s coordinate system point downwards.
Is Kinematic — In 2D Rigidbody component, we had to select in dropdown whether this component will be dynamic, kinematic, and static. Here in 3D, we only get the option to set Kinematic true or false. If set to true then Rigidbody will stop reacting to gravity, applied forces, collisions and joints. Kinematic body also affects motion of other rigidbodies through collisions or joints. Then it can fully controlled by animations or scripts. For example, we can control an elevator with a script via transform property instead of using other objects to push and pull it.
void Update()
{ // player position in x and z axis in Vector3 coordinates
playerMovementInput = new Vector3 (Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
// camera position in Mouse X and Mouse Y axis in Vector2 coordinates
cameraMovementInput = new Vector2 (Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); PlayerControllerFunction();
}
//function to update player position
private void PlayerControllerFunction()
{
Vector3 playerPosition = transform.TransformDirection(playerMovementInput) * speed;
rigidBody.velocity = new Vector3(playerPosition.x, rigidBody.velocity.y, playerPosition.z);
if(Input.GetKey(KeyCode.Space))
{
if(Physics.CheckSphere(feetTransform.position, 0.1f, floorMask))
{
rigidBody.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
}
With Rigidbody, we have other components in 3D also like –
§ Joints —
Each joint have their own specific features to use when connecting two objects. Hinge Joint, Spring Joint, Fixed Joint, Character Joint and Configure Joint.
§ Ragdoll —
This option is very useful for us when we are using any person like character for the project. Mostly in games, we use Ragdoll because Ragdoll is feature where we can apply real world physics to that characters body like — how our body works in real world, we can do using Ragdoll in Unity. For that we have properly add each point of the body from GameObject’s hierarchy into ragdoll properties. Remember should be in T position.
GameObject >> 3D Object >> Ragdoll
In Conclusion of which one to use for our character in our project, if we want your player have to physics properties. It should behave like real world person then we should use Rigidbody but only issue will be that if we want player to jump them handling the jump will hard using Rigidbody. If you choose Character Controller, then we can control the jump and some other movements without affecting much of gravity properties. Rigidbody gives multiple options for user to have his characters to create an environment with real world physics properties, so we can feel like we are in real world not in virtual world. Character controller is useful for us when we do not want much from physics point of view.