• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tanoshimi2000 · Nov 12, 2019 at 01:16 AM · start

Karting Microgame: Reset or StartRace?

So, I'm playing with this Karting Microgame, and wow it's supremely complex for a learning tool. I'm trying to simply add a menu function to run the race again after the race ends, since it simply freezes everything. I've been an amateur programmer for a lot of years, so I kinda know my way around things, but I can't for the life of me figure out how to reset this after the race is over.

There's a TrackManager.StartGame, and calling that allows you to drive again, but it doesn't initiate the countdown. There's a DirectorTrigger.TriggerDirector which has a event it fires to call TrackManager.StartGame, but calling that gets the countdown, but the race doesn't restart and you can't control the kart.

Clearly, I could just reload the level, but I'd think it would be a simple matter to make one call to a function called StartRace and have the timers reset and the countdown show. Am I missing something?

Anyone know how to start a race with the countdown after the initial race is complete?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by KHerron · Feb 28, 2020 at 02:38 AM

@Tanoshimi2000 I know this post is over 3 months old.
However, I wanted to share my findings, just in case someone else comes here looking for the answer to this question.
It took me a few days to figure it all out, but basically the template was NOT designed for a Reset or Play Again, as this is just a tutorial template.


However, you can make it work. To make this happen, you have to have the following:

  1. A way to know the game has ended, is over, has lost or Won!

  2. TrackManager needs to StopRace, if the player never finished.

  3. Make the game think you are displaying the Menu for the 1st time again.

  4. Create a way to RESET the trackmanagers info for a new race.

  5. Move the Kart back to the beginning spot.

  6. Reset the RaceCountDown Trigger.

  7. Create a way to RESET the Racers timer to zero.

  8. Then relaunch the TriggerDirector.


First you need to alter the "MetaGameController.cs" file.
Make sure you have all of these at the top:

 using KartGame.Track;
 using KartGame.Timeline;
 using KartGame.KartSystems;
 using UnityEngine;

Next, add these "public" items:

 [Tooltip("The TrackManager used in this game.")]
 public TrackManager tManager;  // Allows us to call functions on the TrackManager
 [Tooltip("A reference to the Kart")]
 [RequireInterface(typeof(IMovable))]
 public UnityEngine.Object iKart;  //Gives us a refernce to the Kart for movement and placement
 [Tooltip("A differnen type of reference to the Kart")]
 [RequireInterface(typeof(IRacer))]
 public Object initialRacer;   //Gives us a reference to each racers information, like timer

Also add these variable to the top, they do not have to be public.

 IMovable ikart;
 bool newGame = false;
 IRacer m_Racer;

Now, add this line to the void OnEnable() function:

 m_Racer = (IRacer)initialRacer;

Then, add this line to the void Start() function:

 ikart = (IMovable)iKart;

Also, you can remark the line //HandleMenuButton();
and the Menu will appear when you run the game instead of the game starting automatically!
Pressing the Escape key will "Unpause" and the game will start.

Next, you need to alter the HandleMenuButton() funtion so it looks just like this:

 void HandleMenuButton()
         {
             ToggleMainMenu(show: !m_ShowMainCanvas);
             Time.timeScale = m_ShowMainCanvas ? 0f : 1f;
 
             if (m_FirstTime)
             {
                 raceCountdownTrigger.TriggerDirector();
                 m_FirstTime = false;
             }
             if(newGame)
             {
                 tManager.SetForNewGame();  //sets variable back to beginning
                 tManager.ReplaceMovable(ikart);  //Moves cart back to last Checkpoint
                 raceCountdownTrigger.ResetTrigger();  //Resets the count down
                 m_Racer.ResetTimer(); //Resets the Racers Timer
                 raceCountdownTrigger.TriggerDirector();  //Launch Director
                 newGame = false;  // set to false now that a new game is running
             }
         }

So now, to solve #1, #2 & #3 from above, my karting game has a Gas Gauge that can run empty if you do not reach a fillup. This will cause the game to end by running a new function you need to add to the "MetaGameController.cs" file. In my game, it gets called by my Gas Gauge when it reaches EMPTY. You do not have to do it this way, but you will still need to call this script when your game ends.

 public void EndGame()
         {
             Debug.Log("End Of Game");
             tManager.StopRace(); 
             ToggleMainMenu(true);
             newGame = true;
             m_FirstTime = true;
         }

Now, save the MetaGameController.cs file, and go back in to Unity. Click on the MetaGameController item under the Hierarchy. Now, from under the Hierarchy, Drag the TrackManager over to the Inspector for the T Manager. Also, drag the Kart over to both the IKart and the Initial Racer.

This completes the changes to the MetaGameController


To Solve #4 & #5, we simply need to add a function to the TrackManger.cs file.
Open it, and add the following function:

 public void SetForNewGame()
 {
     m_RacerNextCheckpoints.Clear();
     Object[] allRacerArray = FindObjectsOfType<Object>().Where(x => x is IRacer).ToArray();
     IRacer racer = allRacerArray[0] as IRacer;
     m_RacerNextCheckpoints.Add(racer, checkpoints[1]);
 }

This completes the changes to the TrackManager


We already solved #6 when we altered the MetaGameController.cs file and added to the HandleMenuButton() function.
We just simply called the raceCountdownTrigger.ResetTrigger(); function.


Now, to solve #7, we need to alter 2 files. These will be the Racer.cs and the IRacer.cs.
First Open the IRacer.cs file, and add the following lines to the bottom:

 /// <summary>
 /// Resets the Race timer to Zero (0).
 /// </summary>
 void ResetTimer();

Save this file. Then open the Racer.cs file and add this PUBLIC function:

 public void ResetTimer()
 {
     m_Timer = 0f;
     int m_CurrentLap = 0;
     m_LapTimes = new List<float>(9);
 }

This completes the changes to the Racer and Iracer files


And lastly, #8 was already solved when we altered the HandleMenuButton(). we simply called the raceCountdownTrigger.TriggerDirector(); function.


So now, when your game ends, the main menu should appear, Hitting the Escape key should now initiate a NEW RACE by moving the Kart back to the Start Finish line, resetting all the check points and the timer.

I really hope this helps someone!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tanoshimi2000 · Feb 28, 2020 at 12:26 PM 0
Share

Wow. That is the most thorough answer I've ever seen on here. Glad you figured it out!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

116 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Initialising List array for use in a custom Editor 1 Answer

How to get Unity to ask what project to load every startup? 1 Answer

Making a start screen in Unity 3 Answers

DontDestroyOnLoad() not calling awake/start again 2 Answers

Object reference becomes null between Awake and Start after scene load 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges