I am currently having an issue where enemies are triggering my death script for no reason when they are on the other side of the map. Anyone have any ideas why?
using System.Collections;
using System.Collections.Generic;
using UnityEditor.PackageManager;
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"))
{
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);
}
}
private void Update()
{
Input();
}
private void Input()
{
if (dead == true && lifesLeft > 0)
{
dead = false;
Debug.Log("Dead=true");
}
if (dead == true && lifesLeft < 0)
{
Debug.Log("Died");
Killed();
}
}
private void Killed()
{
SceneManager.LoadScene("Menu");
}
}