Death restart
I am currently working on a 2d top down game. basically when the player touches the enemy, The scene changes to the main menu. what i have so far is
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class Death : MonoBehaviour {
public bool isDead;
void Start()
{
isDead = false;
}
void update()
{
if(collision.gameObject.CompareTag("enemy"))
{
isDead = true;
}
if(isDead = true)
{
SceneManager.LoadScene("MainMenu");
}
}
} enter code here`
Answer by Ricky-Brno · Apr 11, 2020 at 10:30 PM
I'm not really sure what the question is, but checking your code maybe you should consider explore OnCollisionEnter2D, instead your code within Update function that runs each frame (https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter2D.html)
I'm not sure whether you're using isDead for something else, but since you change it to true and right away switching to different scene, it seems unlikely
anyway, what you've described, I would do with 2D colliders (don't forget to attach also rigidbody, otherwise collider will be assumed static and if you are moving it, it will kill your performance)
and within code then something like
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.CompareTag("Enemy"))
{
SceneManager.LoadScene("MainMenu");
}
}
Would i need colider2d and rb2d on player and enemy?
Answer by Supphakon · Apr 14, 2020 at 12:23 AM
Yeah ,you need colider2d and rb2d on player and enemy
Answer by csprogrammer · Apr 14, 2020 at 01:03 AM
Would that help the fact that i die as soon as i start the game?
Your answer
Follow this Question
Related Questions
Animation Help? (2D) 1 Answer
How to make a death pickup 2d 0 Answers
enemy spawner 0 Answers
Any documentation about Turn-Based mechanics? 0 Answers
Sprites only rotate by 90 degrees 0 Answers