If you noticed in the previous article that as soon as you stop moving the Player in a moving platform, it will drop off the platform. That is strange behavior. The quick solution is to parent the Player object to the platform.
The existing collider should be left alone as…
To create a moving platform, you will need to provide the position of the origin and destination which you can provide with empty game objects. It might be easier as well to place an Icon to see where these destinations are:
For the Moving_Platform object itself, we will need to…
If you have implemented it before in one game, you probably will do it in another game. In the past, I created a power up collectible in SpaceShooter: https://gamedevbox.medium.com/creating-modular-power-up-systems-in-unity-b0b012032ef1
I will just need to do the same thing here as well. In this case, the Player will be collecting coins and the screen will be updated with the coins already collected.
The collectible should have a Collider set to Is Trigger and have a Rigidbody.
Using delegates, we simply send a message to any object that listens and then destroy itself:
The logic behind this implementation is if the Player is grounded and presses space, he can jump. But since it’s the first time he’s able to Jump while grounded, we grant him the ability to double jump. As you can see below, if Player presses Space, we add a set velocity to _yVelocity.
If the player is not on the ground, then we need to deduct from _yVelocity to bring the Player down. However, he can double jump if he presses Space. But, if he did double jump, we disable _doubleJump so he cannot press jump again in mid-air and be successful.
This is part 2 of Implementing a Physics based character controller. In the last article, I was able to get the Player to move left and right albeit slowly. It had direction and now we can incorporate speed:
All that needs to be done to create velocity is to multiply direction with speed. Also to make it framerate independent which means that the behavior should be consistent in varying hardware, you need to multiply it with Time.deltaTime.
As for gravity, we can determine if the object is grounded with CharacterController.isGrounded and if it’s not, add a rate of downward change by deducting value on the y-Axis of the direction.
First things first. We want our character to move left and right. We currently have a Player as Capsule with Character Controller and Player script attached.
In the player script, we get a handle to the CharacterController and try to get Horizontal input. Whatever value received from the input will be passed to the Move function of CharacterController. For now, speed is set to 1.
To build something complex, we will need to build the basic components first. For now, will use blocks built from cubes.
As you can see above, all of the platforms are a prefab of Platform of a cube with scale (5, 1, 1).
It’s fine to just use a simple material. We can easily dress the platforms with materials once the functionality is complete.
As an example, here’s a very plain wall in Blender:
We will need to associate the Unity project with the Github repository in order to publish files in the repository.
First, go the directory of Unity project folder. In Windows, I use Git Bash shell for this.
user@LAPTOP MINGW64 ~/OneDrive/Desktop/Unity/2.5DProject
Next, you need to initialize git in your project directory: