So I’ve decided to try and jump back into learning Unity and I picked this book back up, “Sam’s Teach Yourself Unity 2018 Game Development” by Mike Geig. I rolled back from 2019 to 2018.4.22f after experiencing issues, and got all the way to Hour 6 before I got stuck. I’m at the point where I’ve made the landscape and everything for “Amazing Racer”, but when I try to implement the “PlayerRespawn.cs” script onto the WaterHazardDetector, nothing seems to happen. The character crosses the threshold, the screen flicks back to the start for maybe two or three frames, and then I’m back in the water. Furthermore, when I reach the end point, It finishes and gives me the time, but if I try to restart by clicking restart, it just restarts me at the position I finished at, which immediately ends it and I can’t restart it proper without relaunching the game. I get no errors when compiling, though, and I have all the spots filled in, so I don’t understand what the problem is.
The code for PlayerRespawn.cs is:
using UnityEngine;
using System.Collections;
public class PlayerRespawn : MonoBehaviour
{
//A reference to the game manager
public GameManager gameManager;
// Triggers when the player enters the water
void OnTriggerEnter(Collider other)
{
// Moves the player to the spawn point
gameManager.PositionPlayer();
}
}
And the code for the Game Manager is this:
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
public class GameManager : MonoBehaviour
{
// Place holders to allow connecting to other objects
public Transform spawnPoint;
public GameObject player;
// Flags that control the state of the game
private float elapsedTime = 0;
private bool isRunning = false;
private bool isFinished = false;
// So that we can access the player's controller from this script
private FirstPersonController fpsController;
// Use this for initialization
void Start ()
{
// Finds the First Person Controller script on the Player
fpsController = player.GetComponent<FirstPersonController> ();
// Disables controls at the start.
fpsController.enabled = false;
}
//This resets to game back to the way it started
private void StartGame()
{
elapsedTime = 0;
isRunning = true;
isFinished = false;
// Move the player to the spawn point, and allow it to move.
PositionPlayer();
fpsController.enabled = true;
}
// Update is called once per frame
void Update ()
{
// Add time to the clock if the game is running
if (isRunning)
{
elapsedTime = elapsedTime + Time.deltaTime;
}
}
//Runs when the player needs to be positioned back at the spawn point
public void PositionPlayer()
{
player.transform.position = spawnPoint.position;
player.transform.rotation = spawnPoint.rotation;
}
// Runs when the player enters the finish zone
public void FinishedGame()
{
isRunning = false;
isFinished = true;
fpsController.enabled = false;
}
//This section creates the Graphical User Interface (GUI)
void OnGUI() {
if(!isRunning)
{
string message;
if(isFinished)
{
message = "Click or Press Enter to Play Again";
}
else
{
message = "Click or Press Enter to Play";
}
//Define a new rectangle for the UI on the screen
Rect startButton = new Rect(Screen.width/2 - 120, Screen.height/2, 240, 30);
if (GUI.Button(startButton, message) || Input.GetKeyDown(KeyCode.Return))
{
//start the game if the user clicks to play
StartGame ();
}
}
// If the player finished the game, show the final time
if(isFinished)
{
GUI.Box(new Rect(Screen.width / 2 - 65, 185, 130, 40), "Your Time Was");
GUI.Label(new Rect(Screen.width / 2 - 10, 200, 20, 30), ((int)elapsedTime).ToString());
}
else if(isRunning)
{
// If the game is running, show the current time
GUI.Box(new Rect(Screen.width / 2 - 65, Screen.height - 115, 130, 40), "Your Time Is");
GUI.Label(new Rect(Screen.width / 2 - 10, Screen.height - 100, 20, 30), ((int)elapsedTime).ToString());
}
}
}
I’ve also included screenshots where the spawnpoints and finish points are defined with relation to starting, water hazard detection, respawning, and finishing.
I’ve noticed that when I start the game, doesn’t start me at the starting point like I defined, but rather from where my player positioned, and I’m wondering if there’s something I’ve missed, because collisions and resets are kind of important with regards to game development and not exactly something I can just put off, and I’ve spent much longer than just an hour trying to figure out what I’m doing wrong.




