I got working enemies but when I have the tag and trigger on a box with no collision below the map, it just does not work. I will send some screenshots if they are needed of the box but the box really should work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class KillFloor : MonoBehaviour
{
[SerializeField] private Transform player;
[SerializeField] private Transform respawn_point;
bool dead;
private Rigidbody playerRigidbody;
public int lifesAmount;
int lifesLeft, lifesLost;
private void Awake()
{
lifesLeft = lifesAmount;
dead = false;
}
private void Start()
{
playerRigidbody = player.GetComponent<Rigidbody>();
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Enemy"))
{
float distance = Vector3.Distance(player.position, other.transform.position);
if (distance < 4f)
if (playerRigidbody != null)
{
playerRigidbody.position = respawn_point.position;
playerRigidbody.velocity = Vector3.zero; // Reset velocity to avoid unwanted movement
lifesLeft--;
lifesLost++;
dead = true;
Debug.Log("Dead=true");
}
else
{
player.transform.position = respawn_point.position;
lifesLeft--;
lifesLost++;
dead = true;
Debug.Log("Dead=true");
}
Debug.Log("Collided" + other.gameObject.CompareTag("Enemy"));
Debug.Log("Collided 2" + gameObject.name);
StartCoroutine(ResetDead());
}
}
private void Update()
{
Input();
}
private void Input()
{
if (dead == true && lifesLeft < 0)
{
Debug.Log("Died");
Killed();
}
}
private void Killed()
{
SceneManager.LoadScene("Menu");
}
private IEnumerator ResetDead()
{
yield return new WaitForSeconds(1f);
if (lifesLeft > 0)
{
dead = false;
Debug.Log("Dead=false");
}
}
}