For this lab, I decided to follow the Roll a Ball tutorial from Unity.
This tutorial alongside the one we got during the lab were enough to get the basics down. And I did not have any problems whatsoever. But I wanted my implementation to be a bit different.
Making my game unique.
To give my game a bit of IP Paris spiciness, I added a few things.
First of all I want to change the skybox because otherwise my game would look boring and tasteless (it will look cooler in VR too 😉). To do so here are all the steps required :
Go to the asset store and pick the fanciest looking skybox material you’d like
Import your new asset
Go to Window -> Rendering -> Lighting Settings
Go to the Skybox Material menu and change it to your newly imported asset
Congrats, now your game will look a lot cleaner !
Next, I don’t want my player to only bounce on walls and be able to get away with it. To prevent this, I enbale the isTrigger option on my walls and add a code that will restart the game if the player collides with them. I need not to forget to add a relevant tag for the death walls.
1
2
3
4
if (other.gameObject.CompareTag("Wall"))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
To make my game a bit more competitive I added a timer so that each and everyone can compete to complete it the fastest. Since I’m going to modify the value of the WinningText, we need to change a bit the initial code of the tutorial.
We are then adding a timer that will use the update function to be incremented and round it. When we collect everything, we display the final time.
See full code :
if (other.gameObject.CompareTag("Wall"))
{
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;
using System;
using UnityEngine.SceneManagement;
publicclassPlayerController : MonoBehaviour
{
publicfloat speed = 0;
public TextMeshProUGUI countText;
public TextMeshProUGUI winTextObject;
private Rigidbody rb;
privatefloat movementX;
privatefloat movementY;
privateint count;
privateint remaining;
publicfloat timeStart = 0;
privateint finalTime;
privatefloat timer = 0f;
public TextMeshProUGUI Timer;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winTextObject.gameObject.SetActive(false);
}
privatevoid OnMove(InputValue movementValue)
{
Vector2 movementVector = movementValue.Get<Vector2>();
movementX = movementVector.x;
movementY = movementVector.y;
}
privatevoid FixedUpdate()
{
Vector3 movement = new Vector3(movementX, 0.0f, movementY);
rb.AddForce(movement * speed);
timeStart += Time.deltaTime;
Timer.text = "Timer : " + Mathf.Round(timeStart).ToString() + " seconds";
}
privatevoid OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("PickUp"))
{
other.gameObject.SetActive(false);
// Add one to the score variable 'count'
count = count + 1;
// Run the 'SetCountText()' function (see below)
SetCountText();
}
if (other.gameObject.CompareTag("Wall"))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
privatevoid SetCountText()
{
remaining = 12 - count;
countText.text = remaining.ToString() + " Collectibles remaining" ;
if (count >= 12)
{
// Set the text value of your 'winText'
finalTime = (int)Mathf.Round(timeStart);
winTextObject.text = "Congrats it took you " + finalTime.ToString() + " seconds to collect them all. Try again to be even faster ! :)";
winTextObject.gameObject.SetActive(true);
}
}
}
}
And here you have it, an “improved” version of the basic Roll A Ball. Let’s see how it goes :