Answer the question
In order to leave comments, you need to log in
Problem with death in Unity 2D?
And so, there is a code for something that, when a Player collides with an object with the Enemy tag, he dies, and a script for respawn, well, the problem is that when a character collides with an object, he simply rests on it, there is a Circle on the object Collider2D.
Script(Death)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerDeath : MonoBehaviour
{
private bool hasEntered;
private void onCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Enemy") && !hasEntered)
{
Destroy(gameObject);
LevelManager.instance.Respawn();
}
}
}
Script(Respawn)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelManager : MonoBehaviour
{
public static LevelManager instance;
public Transform RespawnPoint;
public GameObject PlayerPrefab;
private void Awake()
{
instance = this;
}
public void Respawn ()
{
Instantiate(PlayerPrefab, RespawnPoint.position, Quaternion.identity);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question