- Home /
Race Game Start and Finish-line,Racing game start/finishline
Hi, I'm in the process of making a racing game, where I've come to the point where I only need to make, a start and finish-line, to the map and it's done. Can anybody help?,Hi, I'm in the process of making a racing, game, and I'm at the point where I just need a finish line, for my first map, and it's done. So I was wondering, if anybody knew how to make a finish line, with 3 laps?
Okay, first of all, I am unsure, as in why did you repeat your question twice? Secondly, its a bit confusing as you didn't mention anything specific, as in you are struggling to the graphical representation of finish line or backend code/functions.
Anyways, I will try to answer them both.
For the graphical representation, just mention if it's 2d or 3d game that you are making and try to search the asset store as I am sure there might be an asset useful to you.
About the coding/logic, try making a box collider with is trigger checked in an inspector view.
Place that box collider on the finish line.
Next, make custom script, which will be a lap counter for your game. Assu$$anonymous$$g, your cars/Player game object will have tag attached to it.
Now, use, ontriggerenter function in the script which you have attached earlier to the box collider. Try to check if the player object crossed the line, So every time, that tagged game object crosses the box collider, you increment the lap counter by 1.
Lastly, if(LapCounter >= 3){//FINAL CODE GOES HERE}
P.s. This is a vague explanation and not the final code to use in your game. But I tried to explain you the logic here. If you still have difficulty understand it, try to provide more information about your game. e.g. 1) Is your game 2D, 3D? 2) what do you want to achieve? 3) Images or short video of your problem?
will might increase the chances of better and quick resolution. :)
Answer by Cycy · Jan 31, 2018 at 10:06 AM
Hi,
You may use a box collider at your start/finish line with a script in it who will count each time the player pass through it. When the counter reach 3 you stop the race.
Tell if you need further explications on how to do it and what to write in the script :)
See ya
Hi, thanks for the info about the box collider, and yes I would like to hear more about the scripting :)
Hi,
if you want we can share a collab, i could help you directly in the project :) tell me if you want.
Other wise, here how i see the script, tell me if you need more explanation or maybe some adjustment for your project :)
string playerTag = "playerTagExemple"; // to recognize the player (via tag)
int lapCounter; // to count laps
int maxNbOfLap; // set how many lap the player has to run before end
void Start()
{
initialization();
}
/// <summary>
/// To set the initial state of racce
/// </summary>
void initialization()
{
lapCounter = 0;
maxNbOfLap = 3;
}
/// <summary>
/// When the player Trigger the box collider at the start/finish line
/// </summary>
/// <param name="_objectWhichPassTheLine"></param>
void OnTriggerEnter(Collider _objectWhichPassTheLine)
{
if (_objectWhichPassTheLine.gameObject.CompareTag(playerTag))
{
lapCounter++;
}
checkLapCounter();
}
/// <summary>
/// Check the lap counter to see how many lap the player did
/// </summary>
void checkLapCounter()
{
if (lapCounter == maxNbOfLap + 1) // +1 because the player will trigger the collider at the start.
{
endGame();
}
}
/// <summary>
/// You end the race when the player reach the max number of lap.
/// </summary>
void endGame()
{
//do whatever you need to.
Debug.Log("The game is ended, the player did 3 lap");
initialization();
}
Hi, Thank you for the script, it looks very promising for my type of game :), but I just need to know, how do I use the "PlayerTag" string (is there a certain way to use?)? and (if this is possible) how do I make it work with two players? and about the "_objectWhichPassTheLine", do I need to change that? :)
Your answer
Follow this Question
Related Questions
Can someone help me with my question with IENumerators in C#? 1 Answer
I need help with a Text Location script (Ho bisogno di aiuto con uno script di posizione del testo) 0 Answers
I need help with a Text Location script (Ho bisogno di aiuto con uno script di posizione del testo) 0 Answers
How to stop sound on trigger exit? 1 Answer
Multiple Cars not working 1 Answer