A character controller is a script that makes your character move. In order for your character to move, you need a script that tells your character controller what to do.
You first need an asset from the Asset Store. Your character has one Sprite renderer. You also need the main camera and various tiles that assemble your level. The platforms should have colliders. In addition, one should Collider be present for the floor and some background graphics.
First you need a character controller. You paste this into a folder of your choice in Unity. Select the player and add the script there.

It looks like this:

Various settings for the character can be made here. You have the option to adjust the jumping force, the ducking speed in the form of “crouch speed”, and the smoothness of movement. Make sure to activate Air Control. Use the “What is Ground” option to set the surface your character runs on. For example, select everything here and then remove the player. Select as at the top right Layer the player.
Then there are three more empty spaces:
Ground Check: It is checked whether the playing figure is on the ground.
Ceiling Check: When you crouch, a check is made to see if there is a blanket over your head
To make the checks possible, you need empty objects that determine what is where. Create a first object via Create -> Empty Object and copy it with Ctrl + D. Move the respective objects to the uppermost section of the figure and to the lower section. Name the upper object Ceiling Check and the lower object Ground Check.

Drag these into the respective slots of the player.

Next, add one Rigidbody Add 2D to add physics to your character.

Your character should have a higher gravity than the other game objects. So increase the gravity to, for example, 3.
In addition, your character should always be straight and not rotate. Therefore choose constraints and set the rotation on the Z-axis.

Next, your character needs a collider, otherwise it will simply fall through the screen. You can work with different colliders according to the figure. This can also make the game feel more realistic. You can use circular colliders and box colliders, for example. You have the option of adding a circle collider. First adjust the box collder and add or adjust the circle collider.


Now you can add your PlayerMovement script:
Go to Add Component–> Enter PlayerMovement and select New Script and Create and Add.

Double-click it to open it in Visual Studio.
If the script is grayed out, right-click on Inspector at the top and select Debug mode. Then you can open the document with Visual Studio.
The whole thing now looks like this:

First, a reference from the PlayerMovement script to the character controller is required. This tells them what should happen to the character.
You need the following line:
public CharacterController2D controller;

Then the Character Controller 2D appears in the Player Movement Script:

There you can drag and drop your character controller script into it.

Would you under void Update () {
controller.Move (); If you were writing, you would tell the system to move your character.
But that is not enough. You have to specify where you want your character to move, as well as the direction and speed. For this the control of the player is necessary. So you add the following:
For the input from the player you need the input class:
Input.GetAxosRaw ("Horizontal")
The value is usually between -1 and 1. That depends on the user input.
The left arrow or the A key stands for -1.
The right arrow or D stands for 1.
This also applies to the controller.
You also need the Debug.Log command:
This Debug.log command sends a message to the console.
At the end the line looks like this:
Debug.Log (Input.GetAxisRaw ("Horizontal"));
The console reacts to the new input. If you press the arrow keys in the respective directions, the console shows the following:

The script now looks like this:

The system then registers the entry.
The next aspect we need is the fixed update. With this input, the computer updates the screen with each frame.
You need the following input:
void FixedUpdate ()
{
// Move our character
}
This section takes care of the transfer of player input to the character.
You can delete void Start.
So you have the following script:

The user input must be transferred from one function to another. For this you need a variable.
You need the following line:
float horizontalMove = 0f;
Then the character should be moved. You therefore write the following under FixedUpdate:
controller.Move (horizontalMove);
With the variable (horizontalMove) you specify how much the figure should be moved.
You also have to tell the system that the character shouldn't crouch or jump.
The first false refers to crouching and the second false to jumping.
controller.Move (horizontalMove, false, false);

Now the speed is needed:
public float runSpeed = 40f;
Now you multiply the horizontal input with the runSpeed.
horizontalMove = Input.GetAxisRaw ("Horizontal") * runSpeed;
The FixedUpdate must also be multiplied by the Time.fixedDeltaTime:
controller.Move (horizontalMove * Time.fixedDeltaTime, false, false);
The function determines that the movement is always fixed in the same value.
Your finished script for the movement will look like this:

Fixed update means that the function can be called countless times without any other result.
When the figure falls, it briefly shifts to the ground. To eliminate this, choose the Rigidbody 2D of the figure and change the Collision Detection from Discrete to Continuous. This adjustment ensures that the figure does not slide into the ground.
The jumping
Now jumping is added. The system has to make the figure jump when you press a certain button.
You need the following line:
if (Input. GetButtonDown ("Jump))

This information must now also be transferred to the FixedUpdate area. For this you need a variable.
For this you need a Boolean operator. This means that the result can only be true or false:
bool jump = false;
In addition, there are the conditions that the jump button must be pressed in order for the character to jump.
So it must be added to the condition that:
jump = true;
The jump variable is added to the fixed update area. If the key is not pressed, the character should no longer hop. So in the fixed update area jump = false; added.
The script looks like this:

Now your figure is jumping.
You need physics material so that your figure does not get stuck on platforms while jumping, but glides smoothly down.
To do this, select Create–> Physics Material 2D in the project window


The following settings are available:

Set Friction and Bounciness to 0.
Select the player and drag the physics material into the material slot on the collider.

If you use more colliders, add this to the other collider as well.
How your character crouches
The inputs for the character movements can be found in the Unity project settings.

Select the gate for jumping and click with the right mouse button on "Jump". Choose Duplicate Array Element.

This creates another input option for crouching your character.

An alternative key can be defined for the keyboard layout. This is the Alt Positive Button or the Alternative Positive Button.
The crouching must now be incorporated into the script:
In the update area: if (Input.GetButtonDown ("Crouch"))
A boolean factor must also be added:
bool crouch = false;
Crouch is added to the Fixed Update area.
Crouching doesn't work in the same way as jumping. You don't want your figure to crouch for a moment and then stop. The crouching should end when the button for crouching is released. Your character should only crouch when you hold down the key.
Now you need the following line:
else if (Input.GetButtonUp ("Crouch"))
{
crouch = false;
You're telling the system that if you don't press a button, your character won't crouch.

Now, when you press the crouch button, nothing will happen because crouching is done through animation.
For crouching to work, select your character's collider and drag it into the Crouch Disable Collider.

This makes crouching possible for your figure. Now your character fits between two levels. However, you still need an animation to crouch.
Originally posted on 2020-04-16 21:22:43.
