- Home /
Reload level on collision
I have spent the last 4 hours trying to figure this out with no luck: I'm trying to get the Player (who is a box) to respawn at a set point immediately after coming in contact with the enemy (who is another box). What do i do?
What have you tried? What didn't work? Show us your code and we might be able to tell what's wrong in it.
Answer by Luckyy122 · Jun 24, 2016 at 12:14 PM
I fixed it. Here's the code:
using UnityEngine; using System.Collections;
public class respawn : MonoBehaviour { private object cube;
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "Player")
{
Application.LoadLevel(Application.loadedLevel);
}
}
} }
}
Ter$$anonymous$$ology Respawn means repositions the player, but you are Realoding the $$anonymous$$ap, that's what Application LoadLevel does, it's not the same thing.
wow that's NOT a solution man, check $$anonymous$$e ins$$anonymous$$d
Answer by YoungDeveloper · Jun 24, 2016 at 09:14 AM
Wow, 4 hours, that's really a lot of time.
But, to help your problem. What is spawn? It's basically a movement to certain position (teleport effect), or an actual new player creation. You move your player back to your position using its transform.position = ... or use Instantiate for an actual new creation.
I would like spawn to be when the player teleports to a location after touching the enemy
Answer by EpiFouloux · Jun 24, 2016 at 09:56 AM
Save the position on your start function :
[RequireComponent(typeof(Collider))]
public class Player : MonoBehaviour
{
private Vector3 original_pos;
void Start()
{
original_pos = transform.position;
}
public void reset_position()
{
transform.position = original_pos;
}
void OnCollisionEnter(Collision coll)
{
if (coll.gameObject.tag == "enemy")
{
reset_position();
}
}
}
and if you want a specific position, set orignal_pos as a public variable or as a Serialized Transform
Your answer
Follow this Question
Related Questions
2D Platformer Reload Game On Collision With Sprite 1 Answer
Nav Mesh Agent won't fall off the platform? 1 Answer
Creating and removing HUDs on Collision 1 Answer
Display a different sprite each reload in the game 0 Answers
How can I stop a rigidbody (Ragdoll) character from colliding with the Character Controller capsule? 1 Answer