Hi,
I am trying to figure out how to write a script that if the player collides with an obstacle they die…
The game is an infinite runner with obstacles the player must avoid if they collide with the object then the game ends. However i have only been learning unity for about 2 weeks and need some help trying to figure out the script
On one of the colliders (probably the obstacle’s) you can check off the “Is Trigger”. Then in the code for the player, simply use this function:
void OnTriggerEnter2D(Collider2D other){
//Die
}
if you are making a 3D game, just remove the 2D from the function.
A convenient thing Unity offers is a way to put tags on game objects. If you want the player to collide with multiple things that do different things, then you can put a tag on them. Tags are right under the name in the inspector. You can click “add tag” and then make your tag (e.g. death, nextLevel, etc.). Then, in your function add this:
void OnTriggerEnter2D(Collider2D other){
if(other.CompareTag("death"){
//Die
}
if(other.CompareTag("nextLevel"){
//Go To Next Level
}
}
Hope this helps.