Help with OnCollisionEnter
Right now I am trying to make a racing game. All I need help with is figuring out which player collided with the finish using OnColisionEnter. How can I distinguish which player hit the finish.
OnCollisionEnter? Whats the finish line a brick wall?
Try OnTriggerEnter.
void OnTriggerEnter(Collider other)
{
Debug.Log(other.gameObject.name);
}
Of course ins$$anonymous$$d of Debug.Log you could just save the name in a variable so you know which one it is or reference a script that hold the unique number or name of the car (if all your cars are created from a prefab and all called car then getting the name isn't a good idea).
I'm a bit confused about the text between the parameters. How do I make it specify to a certain game object? It looks as if it is only letting colliders trigger it but I am probably wrong. Please explain how I assign a certain object into the parameters.
Put that script on a box with a collider and stretch it across the finish line, tick is trigger so you can drive through it but it'll trigger if something with a collider goes through it.
Now drive your cars through the trigger and see what the Debug.Log sends out to the console. Like I said if all your cars are called the same thing then it's doesn't really help but you could put a script on each car and store a number or name in that script and retrieve that.
What OnTriggerEnter does is activate if something hits the edge of the collider, but it doesn't stop the thing from going through it.
The (Collider other) bit says store the collided object in a variable called "other".
Then Debug.Log just writes out to the console (the thing that shows script errors and such) we ask it to output
other = the thing the trigger collided with
.gameObject = the GameObject part of that thing
.name = that GameObjects name