After testing I now need to make one final change. I want to add UI instructions within the game so that the player would know that if they press the space twice the character double jumps.
I selected the Hint GameObject under the UI GameObject and changed the text in the Text component. I did this for every scene.
To change the platform colour from red to blue in the city theme I duplicated the FloatingPlatform prefab and renamed it FloatingPlatformBlue. I also duplicated the ground sprite in Photoshop and changed it to blue.
Finally on the Player – City Prefab ’s PlatformSpawner child, I assigned this newly created FloatingPlatformBlue prefab to the PlatformPrefab field .
I’ve just tested the game and as a result I need to make the following changes;
Change the game so that the player slows down when they hit the platform instead of stopping altogether.
When the player collides with an obstacle it decreases the current speed of the character, and it slowly accelerates back to the max speed.
In the CharacterRun script I added two variables: CurrentRunSpeed and acceleration
I also renamed the runSpeed to MaxRunSpeed.
Then I inserted the following lines in the CharacterRun script Update function. This will speed up the character if its current speed is less than max speed.
I need to change it so that instead of spawning you back to the beginning it spawns you where you die so you can get through the level quicker.
The next change is to make an Obstacle script. When the player collides with the object, the script will decrease its current speed to 75%.
public float SlowMultiplier = 0.75f;
void OnCollisionEnter2D(Collision2D coll)
CharacterRun player = coll.gameObject.GetComponent<CharacterRun>();
if (player == null) return;
player.CurrentRunSpeed *= SlowMultiplier;
I added the Obstacle script to every obstacle prefab.
In the city theme, change the colour of the the platform from red to something easier to see.
I made the jump speed quicker/height quicker. There was a slight delay between when I pressed the keypad and when the character jumped. I changed the CharacterRun a little bit, doubled the gravity and changed the jump to rigid body.Addforce method. It was a bit better but not great. I don’t know enough coding at the moment to make it any better.
I have deleted the assets in Unity that I don’t need and I reorganised the Project Folder in Unity (as suggested by James). Unity updates can cause a lot of bugs if I have a lot of unnecessary assets.
I put every unused or redundant asset in a new folder instead of deleting them. I created the following folders:
All the animation clips are kept here (except the player animation, which goes into the subfolder).
– Animator Controllers
– Player: includes every animation clip that is connected to the player.
I reworked every scene, and created a prefab from every ‘master’ object. The objects I was calling ‘master’ were basically prefabs and I stored them in the scene. As prefab I can store them in the project folder which makes it easier to reuse them, for example, in case of the coin. This way I can setup a single coin prefab and use it on every scene.
– Player : The player prefabs are in this folder
– Spawnables: All the spawnable elements are in this folder. I made a subfolder for every theme, but I put the objects that are shared between scenes (platforms, grounds, coins) in the root Spawnables folder.
I changed the organization in the scene hierarchy, based on the current practice in the Unity Community, there are empty game objects with lots of ’ — ’ characters that are working like section headers. This is quite a nice way to seperate different element of the scene.
I removed every Master object from the scene after I created a prefab from them.
I added a new Background Music game object with an audio source on it.
Player Prefabs & Skins Reworked
Under the Prefabs/Player folder there are several player prefabs. The _Player – Master is the main prefab, the others are just copies of this, tweaked to match the given theme.
I added the jump sprites to the game.
I made an empty Start State for the animator. This doesn’t contain any animation, and it works like a hub when the game starts. The transitions are using the Player Skin variable to determine which should be the next state.
I set the Run State names to match their theme. Player Skin can be set on the Character Run script inspector, the script will update the animator when the game starts.
I made the jump fairly simple. If the animator IsGrounded parameter is false, the animator plays the Jump animation, otherwise it plays the Run animation.
Some more problems…
3. I need to connect the themes. To do this I went into the build settings and put the scenes in the order I wanted the player to follow. I made a 2D invisible hit box so that at the end of the level, when the player runs into it, he will automatically go to the next level.
The problem occurred because of two things. First, the platform spawner (actually every spawner) should be a child of the player character. I placed them before the player, out of the camera viewport, and offsetted them from each other. This way the spawners will follow the player movement and the Spawn Rate setting will result in the right functionality.
I changed the spawner a little, a single spawner script, but multiple spawn points allows better control on the spawning. The spawner now randomly selects a spawn point from the list adding more variety.
5. Another issue is that the doctors and nurses are too fast and then too slow overtaking the character. I need to get the balance right.
Because of the mover script, the player was pushed back by the obstacles. This is why the doctors and nurses look like they’re slowing down when the player started to run again. I added a new float variable (Speed) to the medmove script. With it I can fine tune and correct the speed of these characters.
6. I need to slow down the platforms and coins because they’re just too fast to react to.
These objects had a Mover script on them and it moved the object towards the player. Since the player was moving too, the player movement looked much faster. I removed the mover script, after all in real time the wall wouldn’t run towards you so neither should it in the game world.
Also without the mover script, the objects won’t push the character back, and this also solved the speed problem (2).
7. Coins are taking too long to appear in each theme
Fixing the fourth problem solved this. I can control it with the spawn rate variable on the spawner script.
8. Player/character hitting obstacles
On the Rigidbody2D component there is a constraints section. Freeze Position will prevent the movement of the object when it collides with something in the given axis. The Freeze Rotation will prevent the object bouncing and fall over when it collides with something.
I turned these settings on for every obstacle.
9. I need to add the sound effects later so I added a new empty game object with an Audio Source on it and named Background Music for every scene. I can assign background music here.
I added the coin collection audio to the coin itself, and made the script play when the coin is picked up.
10. The coin collection doesn’t work in some of the themes
When the Time to Destroy arrives the Collect script will automatically destroy the coin. This way the Audio Source on the coin will have time to play the sound before the game object is destroyed.
The coin spawner dynamically adds a Destroyer component to the spawned coin. This component will destroy the coin if it’s not picked up and it’s out of the camera viewport.