using UnityEngine;
public class Teleporter : MonoBehaviour
{
public Transform teleportDestination; // where to teleport
private void OnTriggerEnter(Collider other)
{
// Check if the object entering is the player
if (other.CompareTag("Player"))
{
Debug.Log("teleport");
performTeleport(teleportDestination);
// If player has CharacterController, disable it before teleporting
}
}
public void performTeleport(Transform teleportPoint)
{
GameObject player = GameObject.FindWithTag("Player");
CharacterController cc = player.GetComponent<CharacterController>();
if (cc != null) cc.enabled = false;
player.transform.position = teleportPoint.position + new Vector3(3, 3, 0);
if (cc != null) cc.enabled = true;
}
}
update the enemyHealth, when the boss die direct teleport
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public float maxHealth = 50f;
private float currentHealth;
public bool isBoss = false;
public GameObject player;
public Transform teleportPosition;
void Start()
{
currentHealth = maxHealth;
}
public void TakeDamage(float amount)
{
currentHealth -= amount;
Debug.Log(gameObject.name + " took " + amount + " damage. HP left: " + currentHealth);
if (currentHealth <= 0)
{
Die();
}
}
void Die()
{
Debug.Log(gameObject.name + " died!");
// Optional: play death animation
// animator.SetTrigger("Die");
Destroy(gameObject); // remove enemy from scene
if(isBoss)
{
Debug.Log("boss teleport");
Teleporter teleporter = GameObject.Find("teleportPlatform").GetComponent<Teleporter>();
teleporter.performTeleport(teleportPosition);
}
}
}
add your teleport object
remember to add collider and check the is Trigger
can make your collider slightly bigger
create empty for your teleport position
in your editor, attached the point that you want to teleport
after fighting the boss it will teleport
public Transform teleportPosition =null;
Teleporter teleporter = GameObject.Find("teleportPlatform").GetComponent<Teleporter>();
teleporter.performTeleport(teleportPosition);
this is the code block that you want to teleport. attached to the part that you want