At the beginning, add a new script to your character. Name it PlayerMovement Script. First we need a reference from the Player Movement Script to the Character Controller. The script is told to move the character.
At the beginning we create a variable:
public CharacterController2D controller;
Then you have the option of dragging the character controller to a free space.
The direction and the speed of the movement must be defined for the movement. For this you need input from the player:
Left arrow key or A = 1
Right arrow key or D = -1
It's the same with the controller.
horizontalMove = Input.GetAxisRaw ("Horizontal");
The update area organizes the input from the player.
FixedUpdate is used for physics. This function is required.
Create another variable:
float horizontalMove = 0f;
FixedUpdate moves the character.
void FixedUpdate()
{
controller.Move (horizontalMove, false, false);
}
The first false stands for that the player should not crouch and the second for that he should not jump.
A definition for Move is required in the Player Controller Script:
public void Move (float move, bool crouch, bool jump)
{}
As the next variable you need:
public float runSpeed = 40f;
The input from the player is multiplied by the running speed:
horizontalMove = Input.GetAxisRaw ("Horizontal") * runSpeed;
If the player moves left and right, the move will be -40 and 40.
The movement is also multiplied by Time.fixedDeltaTime.
Time.fixedDeltaTime is the time since the last FixedUpdate function was called. This specifies that the movement takes place by the same amount, no matter how often the function is called. Thus, the speed of the character is the same, no matter what systems or platforms it is.
If the pawn falls, it can shift a little into the ground.
That is why in Rigidbody2D the collision detection changes from discrete to continuous.
For jumping, it is checked whether we press the jump key or not:
if (input.GetButtonDown ("Jump"))
{
jump = true;
}
This should also be done in the FixedUpdate function. The information is transferred with a Boolean variable:
bool jump = false;
The jump variable is also added under FixedUpdate.
When the jump button is pressed, the PlayerMovement script forwards to the controller that we want to jump. After that it will be again
jump = false;
This needs to be defined as we don't want to skip ahead forever.
Another problem is that when the character tries to jump onto a platform, it gets stuck there. The character should then slide down and fall down on this. A physics material is required for this. Under Project, right-click Create–> PhysicsMaterial2D
Name it Slippery. Set Friction and Bounciness to 0. Take the PhysicsMaterial and drag it onto the material from the player's BoxCollider.
If you have defined several colliders for the protagonist, you can also assign the physics material to the other colliders.
The keyboard layout is defined via the Unity InputManager. You can find this under the Project Settings.
Right click on the jump button and select Duplicate Array Element. Name it "Crouch".
If the players want to play with arrow keys, simply define an alternative button.
Repeat the steps from jumping to crouching:
When the crouch button is released, the character should also stop crouching. So it doesn't work quite like jumping. If the players want to play with arrow keys, simply define an alternative button.
Add the following in the update function:
else if (Input.GetButtonUp ("Crouch"))
{
crouch = false;
}
So that the corresponding sections appear in the character controller, you have to switch them from private to public:
Pull that Colliderwhich should be deactivated in the corresponding field. If there is a “ceiling” above you, your character will slow down when you press the crouch button.