- Home /
i have a 2d infinite runner game and I want to get the position of the nearest platform so I could respawn the player there.
hello I have a 2d infinite runner game, and I want it to respawn to the nearest platform. I actually already created a respawn method but It is very wrong, I just add 10 to the y and x position of the player
PlayerPrefs.SetFloat("XCheckPoint",(Player.position.x+10));
PlayerPrefs.SetFloat("YCheckPoint",(Player.position.y+10));
the tag of the platform is invisiblePlatform
the Player is Instantiated so there might be a problem if you declare something that you must drag and drop.
Basically all I want is just to get the position of the nearest platforms
Alternatively, you could implement the OnCollisionEnter() method inside the player's script.
Whenever you collide with a platform, store the platform that the player has collided with inside the script. When you need to respawn, just respawn the player on the last platform that he has collided with.
Some pseudocode:
GameObject lastPlatformTouched;
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.tag == "invisiblePlatform") {
lastPlatformTouched = collision.gameObject;
}
}
Your answer
Follow this Question
Related Questions
Help with moving AI object to nearest spawned objects position 0 Answers
transform.position.x ? 1 Answer
AI Attack Not Working! W/Video 1 Answer
CompareTag being ignored? 1 Answer