Rookie Awards 2024 - Open for Entries!
Adventure Trip
Share

Adventure Trip

Joan Giner Pavia
by Jhonny6 on 28 Sep 2023

"Adventure Trip" is an exhilarating side-scrolling 2D platformer that thrusts players into an action-packed adventure on a mysterious island teeming with dangers and treasures.

1 250 0
Round of applause for our sponsors

For the realization of this videogame my partner and I had to do all the art, rigging, animating and programming the whole videogame in 3 weeks.

Before we start with the explanation of the video game code, first let's put a gameplay.

Below I will leave two links, one from itch.io so you can download the game and another from github so you can download the project or see how it is organized.

Itch.io: jhonny6.itch.io/adventure-trip

Github: github.com/Jhonny6fn/Adventure_Trip

Character Controller

We are going to explain the most important code of the video game, and the one that makes everything work correctly, and we are going to start with the code that composes the character.

Variable Declaration:

Public and private variables are declared to configure and control the character's behavior. Some of these variables include Trigger (a GameObject for triggering), speed (the character's speed), jump (the force of the jump), FXjump and FXtrigger (jump and trigger sounds), among others.

Awake Method:

Used to initialize some variables, such as the AudioSource component and the flags jumpsound and triggersound.

Start Method:

Used to set other variables, such as the CheckGround component, the Animator component, and the Rigidbody2D component of the character. Some flags related to the character's state are also initialized, such as estoyatacando, estoysaltando, and estoytocandandosuelo.

Update Method:

It contains the main logic for controlling the character.

First, the Z position of the character is updated to make sure it is in the correct plane.

Then, it is checked if the character can move (canMove).

If the player is not pressing any movement keys and is not attacking or jumping, an idle animation is played.

If the player presses the fire button (Fire1), a Fire object is instantiated at the character's position, and a fire animation is played.

If the player presses the horizontal movement keys, the character moves in the corresponding direction and a running animation is played.

It is checked if the character is in contact with the ground (collider) to allow the character to jump. If the player presses the Jump button and is on the ground, the character performs a jump and a jump sound is played.

If the player presses the jump button but is not on the ground, a debug message is displayed.

Apart from the character controller we will also look at the Checkground, platforming, shooting and life.

Code 1 (CheckGround):

This script is responsible for checking whether an object (possibly the player character) is in contact with the ground or with platforms. The key aspects of the code are:

public static bool colFeet;: This static variable is used to indicate whether the object (usually the character's feet) is in contact with the ground or a platform.

private void OnTriggerEnter2D(Collider2D collision): This method is called when the object comes into contact with another object (through collisions). If the colliding object has the label "Ground" or "Platforms", the variable colFeet is set to true, indicating that the object is on the ground or a platform.

private void OnTriggerExit2D(Collider2D collision): This method is called when the object exits contact with another object. If the object that has been lost has the label "Ground" or "Platforms", the variable colFeet is set to false, indicating that the object is no longer on the ground or a platform.

This script is useful for tracking whether the character is touching the ground or a platform, which can be important for determining whether the character can jump, among other actions.


Code 2 (Colision_Platform):

This script is used to handle collisions between the object to which the script is attached and the platforms. Here are the key parts:

public static static bool soundPlatform;: This static variable is used to control whether to play a sound when the object collides with a platform.

private AudioSource Music;: An AudioSource component is used to play sounds.

private void OnCollisionEnter2D(Collision2D collision): This method is called when the object collides with another object. If the colliding object has the label "Platforms", a sound is played using AudioSource.PlayClipAtPoint, and the soundPlatform variable is set to false. In addition, the object containing this script becomes a child of the object it collides with, which is often used to make the object move along with the platform.

private void OnCollisionExit2D(Collision2D collision): This method is called when the object stops colliding with another object. In this case, the object is detached from any parent object (usually the platform) by setting the parent to null.

This script allows to control collisions with platforms and to handle sound playback in response to these collisions.


Code 3 (Shoot):

This script is used to control the behavior of a forward moving object (usually a projectile). Here are the key details:

public float speed;:: This variable determines the speed at which the object moves forward.

void Update(): In the Update method, the object moves forward in each frame using transform.Translate.

private void OnTriggerEnter2D(Collider2D collision): This method is called when the object comes into contact with another object. If the object with which it collides has the "Enemy" label, the destroyme() method is called, which destroys this object.

public void destroyme(): This method is used to destroy this object.

private void OnBecameInvisible(): This method is called when the object becomes invisible in the camera. In this case, the object is destroyed.

This script is typically used for objects such as projectiles that advance and can be destroyed by colliding with enemies or leaving the camera view.


Code 4 (Life):

This script is used to manage the life of an object, possibly the player character. Here are the key details:

public float Life;: This variable represents the amount of life of the object.

public Slider barLife;: This seems to be related to a life bar in the user interface.

void Update(): In the Update method, the life bar is updated to reflect the Life value. If Life reaches zero or less, the script loads a scene called "Menu_Start" and destroys the object to which it is attached (possibly the player character).

This script is used to track and display the life of the object and take action when the life runs out.

Enemy

Now I am going to explain the three different enemy codes: the one of the final boss, the one of the Indian and the one of the Indian following you.

Variables and Components:

private Animator myanimator: This variable stores a reference to the Animator component attached to the object. It is used to control the animations of the boss enemy.

private static bool deathsound: This static variable is used to control whether to play an enemy death sound. Being static, the variable is shared among all EnemyBoss instances in the game.

private bool OnnOff: This variable is apparently not used in the code and may be unnecessary.

private AudioSource music: This is an AudioSource component that is used to play sounds related to the boss enemy.

public AudioClip FXDeath: This is the sound that is played when the boss enemy is defeated.

public int VidaEnemy = 30: This variable determines the amount of life of the boss enemy and starts with a value of 30.

Awake Method:

It is used to initialize some variables, such as the AudioSource component and the soundDead variable, which is set to false at startup.

Start Method:

It is used to configure the Animator component and the OnnOff variable, which is set to true.

Update Method:

In this case, myanimator.Play("Animation_Idle") is called, suggesting that the enemy is in a waiting state.

Then, it is checked if the boss enemy's LifeEnemy is equal to or less than zero. If so, the boss enemy is destroyed (Destroy(this.gameObject)) and a death sound is played (AudioSource.PlayClipAtPoint(FXDeath, transform.position, 0.2f)). The variable deathsound is set to false, but its purpose is not clear in this context.

OnTriggerEnter2D Method:

This method is called when the boss enemy collides with another object with a collider in trigger. Two possible collisions are detected here:

If the colliding object has the "Shot" tag, the boss enemy's LifeEnemy is reduced by 1.

If the colliding object has the "Player" tag, the life component of the player object is accessed and its life (Life) is reduced by 1. This implies that the boss enemy can damage the player when colliding with it.

Another Enemy Script

Variables and Components:

private Animator myanimator: This variable stores a reference to the Animator component attached to the object. It is used to control enemy animations.

private static bool soundDeath: This static variable is used to control whether to play a death sound when the enemy is destroyed. Being static, the variable is shared among all EnemyScript instances in the game.

private bool OnnOff: This variable is apparently not used in the code and may be unnecessary.

private AudioSource music: This is an AudioSource component used to play enemy-related sounds.

public AudioClip FXDeath: This is the sound that is played when the enemy is destroyed.

Awake Method:

It is executed at the start of the game and is used to initialize some variables, such as the AudioSource component and the soundDead variable, which is set to false at startup.

Start Method:

It is used to configure the Animator component and the OnnOff variable, which is set to true.

Update Method:

In this case, myanimator.Play("Animation_Run") is called, which implies that the enemy is in a state of constant motion.

OnTriggerEnter2D Method:

This method is called when the enemy collides with another object that has a collider in trigger mode. Two possible collisions are detected here:

If the colliding object has the "Shot" tag, the enemy is destroyed (Destroy(this.gameObject)), which implies that the enemy is killed by being hit by a projectile. Then, a death sound is played using AudioSource.PlayClipAtPoint(FXDeath, transform.position, 0.2f), and the soundDeath variable is set to false.

If the colliding object has the "Player" tag, the life component of the player object is accessed and its life (Life) is reduced by 1. This implies that the enemy can damage the player when it touches him.

Pirate Follow

Variables:

private GameObject principal: This variable stores a reference to the GameObject "Pirate", which is the object we want to track.

public float speed: This variable determines the speed at which the object containing this script will move towards the object "Pirate".

Start Method:

The GameObject named "Pirate" is found using GameObject.Find("Pirate") and stored in the main variable. This is done at the beginning of the game to make sure we always have a reference to the main character we want to follow.

Update Method:

Here we implement the main logic for the object containing this script to follow the main character.

transform.position represents the current position of the object containing this script (the object we want to follow the "Pirate").

Vector2.MoveTowards is used to gradually move the position of the current object towards the position of the "Pirate". Three arguments are passed to this function:

The current position of the object (transform.position).

The position we want to move to (main.transform.position which is the position of the "Pirate").

The speed at which we want it to move (speed * Time.deltaTime).

speed * Time.deltaTime ensures that the movement is smooth and time-dependent so that it is constant in different systems and frame rates.

Other

And finally I will explain the two remaining codes which are a parallax and a random spawn.

Parallax

Variables:

private float length, startpos: These variables store the length of the background image and its initial position on the X axis.

public GameObject cam: This is a reference to the main camera of the game. You must assign the main camera to this variable in the Unity Inspector.

public float parallaxEffect: This variable determines the speed at which the background moves relative to the camera. A larger value will increase the scrolling speed, while a smaller value will decrease it.

Start Method:

In this method, the variables startpos and length are initialized:

startpos is set as the starting position on the X-axis of the object containing the script. This is used to calculate the displacement relative to the start.

length is obtained using the SpriteRenderer component attached to the object. It represents the length of the background image on the X axis.

FixedUpdate Method:

The FixedUpdate method is executed at regular intervals and is used to implement the parallax effect.

float dist calculates the distance the camera has traveled on the X-axis multiplied by the parallaxEffect factor. This determines how much the background should be shifted relative to the camera. The higher the value of parallaxEffect, the faster the background will move.

transform.position is updated with a new position on the X axis calculated by adding startpos (initial position) and dist (the offset relative to the camera). The Y and Z coordinates of the object are not changed, which means that the background moves horizontally, creating the parallax effect.

Random Bomb Spawn

Variables:

public GameObject bomb: This is a reference to the prefab of the bomb (or randomly generated object) that will be created on the stage. You must assign the prefab in the Unity Inspector.

public int xPos: This variable is used to store the X position where the pump will be randomly generated.

public int Counter: This variable is used to keep track of the number of bombs that will be generated. You can set the initial value in the Unity Inspector.

public float wait = 0.5f: This variable determines the waiting time between the generation of consecutive pumps.

Start Method:

The Start method is executed once at the beginning of the game. In this method, StartCoroutine(BombaDrop()) is called, which starts the random generation of bombs as a routine (coroutine). This allows bombs to be generated continuously over time.

BombaDrop Method(Coroutine):

This method is a routine that is responsible for randomly generating pumps. It runs continuously as long as Counter is greater than or equal to 0.

In each iteration of the while loop, a random X position is generated in the range between 20 and 99 (you can adjust these values according to your needs) and stored in the variable xPos.

Instantiate is then used to create an instance of the prefab pump at position (xPos, 10, 0) of the stage. The Y position is set to 10 and the Z position to 0. This determines the height at which the pump will be generated in the game.

After the pump is created, the code waits for a specified time, specified by wait, before continuing to the next iteration.

Finally, the Counter value is incremented by 1 to keep track of the number of bombs generated.

My Website

And finally, here is a link to my website:

Wix jdfnadj


Comments (0)

This project doesn't have any comments yet.