- Home /
Question by
JohnWilms3 · Feb 07, 2018 at 06:44 PM ·
collisiondeath
How to use collisions to restart a scene
Im making a simple platformer. In it i have black blocks that should restart the scene if touchedby the player. How do i achieve this. Here is my player code //Movement public float speed; public float jump; float moveVelocity;
//Grounded Vars
bool grounded = true;
void Update()
{
//Jumping
if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Z) || Input.GetKeyDown(KeyCode.W))
{
if (grounded)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jump);
}
}
moveVelocity = 0;
//Left Right Movement
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
moveVelocity = -speed;
}
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
moveVelocity = speed;
}
GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, GetComponent<Rigidbody2D>().velocity.y);
}
//Check if Grounded
void OnTriggerEnter2D()
{
grounded = true;
}
void OnTriggerExit2D()
{
grounded = false;
}
}
Comment
Answer by mgsirotti24 · Feb 08, 2018 at 01:42 AM
Try putting this in a collision/trigger function:
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
Make sure to include
using UnityEngine.SceneManagement;
Your answer
Follow this Question
Related Questions
Very simple collision death 1 Answer
How to restart the level upon cantact 1 Answer
Cant Find Destruction Code? 1 Answer
My Player dies only when he wants to?!? 1 Answer
Can't display the menu when player dies 2 Answers