top of page

Scripts

In this section i am mainly going to talk about the scripts that i created for the project.

The Background Scroller Script

As i stated earlier the game is an infinite runner, that means that the map is endless, at least from the perspective of the player. The map is created out of 5 main components. Those are 3 backgrounds a dark city background , a light city background and a city foreground those 3 move at different speeds, giving the effect of a 3D city. There is also a street component and a light post component. Those 2 don't have anything special to them except the light posts which are placed in front of the player to give the impression of a 3D world. As i was saying the map is not endless, those components just "scroll" this also giving the name of the script. What i mean by that is that when the element is not in front of the player anymore it just resets its position through a smooth transition.

The script works in a very simple way. It uses 3 variables speed, distance and startPos. In start I assign to startPOs the position where the object is at the beginning of the script. In update i have an if statement witch checks whether or not the acceleration on the x axis is higher than 0, the acceleration value is given by the accelerometer sensor of the device, if it is then the background will move using the transform.Translate function, which basically "teleports" it to the left the only difference between the if statement and the else statement of this function is that in the if one it also multiplyes the speed with the acceleration value and with 10 to give it more stages of acceleration. The update function ends by resetting the position of the object when this has moved a certain distance.

Untitled.jpg

The same thing also applies to the car movement script. If the acceleration is lower or equal to 0 then the car will start moving towards the right and if the acceleration is not equal or lower than 0 the car will move slowly to the left of the screen. Please note that I only worked on the second if statement in this script.

Untitled.jpg

The Random Spawner Script

The random spawner script is essential for the game. This script has seen a number of iterations in the game ranging from spawning enemies, power ups, maps, characters to small little purple blobs. This is a better version of a spawner script that i found online"https://www.youtube.com/watchv=ao_BZMORqQw"I modified it so that it will work with an unlimited number of objects. Please note that the original was only able to use 5 objects. It uses a string of objects that are attached to it in the editor, a spawnRate value that determines the interval of time between spawns, this is a public variable for an easy acces in the editor, a nextSpawn variable that uses the spawnRate and Time.time variables to decide when to spawn the next object.

In the update function there is an if statement that checks if Time.time is higher than nextSpawn and if that is the case, the whatToSpawn variable will be assigned a random number between 0 and the length of the string objects, remember that i wanted to be able to put an unlimited number of objects in the spawner. I have been using the word spawn until now even though i don't really do that, I actually instantiate the objects using the Instantiate function that chooses the object from the list that has the number chosen randomly, at the position of the spawner object, and with the same rotation in space as the spawner object. At the end of the script nextSpawn takes the value of Time.time + spawnRate and then the script repeats itself.

Untitled.jpg

Example of the spawner

The Move Destroy Script

First thing first this script does not destroy anything, it was supposed to but i had some problems and the name just grew on me so i kept it. This script only has one job and that job is to move the spawned objects to the left, towards the player, you can see that in the example above.

It is a very simple script. It only uses 2 variables, one of them is the speed of the object and the other is the rigidbody component attached to the object. Both of them are public to allow a easier utilization of the script, but due to the fact that the script requiers the rigidbody component to work it also searches for it in the start function in case the user forgot to attach it. In the function FixedUpdate I create a new variable movement witch is a vector2 meaning that it only works for 2 axis the x and y. In the final of the function I add force to the object based on its movement direction and on its speed. The AddForce function is very interesting more so when it is in a fixed update, it will keep making the object faster based on how long it takes to get to the character.

Untitled.jpg

The Delete Script

This is the script that actually deletes the spawned game objects so we won't hava a trillion of them pilling up behind the character. Plese note that the character was supposed to be able to jump, whether or not that is working I had to make this functional for that, there are also scripts attached to the character that will delete the game objects in case the character will collide with them. The script is attached to a collider object that is behind the character. 

The script looks for a collision with a game object, saves that collision in col and then checks if the game object tag is either Enemy either Chaos and then deletes it. I know that i could have had only one check with only one if but i like everything looking nice and tidy. Also we may touch again on what is chaos if i have enough time to finish it.

Untitled.jpg

The EnemyKill Script

EnemyKill / Punch this is basically the same.  This script detects when the user clicks on the screen and sets active a collider object that has attached to it the Destroy script and runs a punch animation. It also counts down and dezactivates the collider object. 

Untitled.png

The Main Menu

The menu of a video game is like a map so, that is why i made the menu exactly like an actual map. It has 5 buttons : play, settings, shop, character selection and stats. I used a simple script to change the scene when you press the button. The script is the standard SceneManager type of script it is the basic way to do it. It makes use of the event system. Also every scene has a scene manager game object to witch i attached the scene loader script. Also all the scenes have a map button that takes the player back to the main menu.

Untitled.png

Work in progress

In my opinion one of the most important aspects of a video game is the gamplay and that was our main focus when we made the game but great gameplay works best with a great story, and that was exactly what i wanted most to add the I wrote the next two scripts exactly for this reason. The idea was that the character would collect the little purple blobs and there were 5 "Landmarks" and the game map would become more and more "corrupt" and at 40 a new button would appear, a button that would take the player to a boss battle level, unfortunately i didn't have enough time to finish it.

y.png
i.png
bottom of page