- Home /
C# - Race Game Finish-line
Hi there.
I am working on a simple race game but I got into kind of a issue:
The player can cheat around the finish line by going forth and back, since I make use of a simple OnTriggerEnter method. Is there a way to force the player to complete the whole lap?
Thanks.
Answer by FadiLoutf · Nov 29, 2017 at 05:16 PM
What about to use some checkpoints , so when the player reach to finish line, you will check if he Trigger (across) all checkpoints in the order or not ... put the last check point near to the finish line
I hope this idea help you ^_^
Answer by connorwforman · Nov 29, 2017 at 05:24 PM
Sort of like what @FadiLoutf said, here would be the script that could do that. And, you don't need that many colliders. A total of 3: The finish line collider, the finish line activation collider (just before the finish line), and a non isTrigger solid collider on and wider than the finish line. This is what it would look like(the == is the road): =====FinishLineActivationCollider(attatch script to this)===ThickColliderHalf|FinishLine|ThickColliderHalf
Script:
//this would go on the end activation collider
public GameObject StartCollider_Solid;
void Start () {
StartCollider_Solid.SetActive(true);
}
void OnTriggerEnter (Collider other) {
if (other.gameObject.name == "Player") {
StartColliderDeac();
}
}
void StartColliderDeac () {
StartCollider_Solid.SetActive(false);
}
Answer by pbakker · Nov 29, 2017 at 05:36 PM
=== EDIT: IGNORE THIS, DOESN'T WORK! ===
Nevermind, solved the problem. For the guys who are curious:
int negativeLaps = 0;
int minRotation = -136f;
int maxRotation = 43f;
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "finish")
{
if(GetComponent<Rigidbody2D>().rotation % 360 < maxRotation && GetComponent<Rigidbody2D>().rotation % 360 > minRotation)
{
if(negativeLaps == 0)
{
//player completed lap legitimately, do stuff as usual
}
else
{
negativeLaps += 1; //player restored 1 lap of backwards driving
}
}
else
{
negativeLaps -= 1; //player cheated and drove backwards
}
}
}
I simply test if the player is driving in the right direction by having a minimum and maximum rotation value. If the player drives in the wrong direction, 1 negative lap is added. This forces the player to compensate all the laps he drove backwards.
Your answer
![](https://koobas.hobune.stream/wayback/20220612143620im_/https://answers.unity.com/themes/thub/images/avi.jpg)