I have looked up a guide on how to respawn a player after they collide with an object (water/outside of terrain), and it works perfectly. But I am trying to attempt to do the same when their HP is reduced to 0. (In context, the scene loads at one point, and the respawn point is in a small building)
Placing an invisible 3D box and disabling mesh for the colliding part works fine and all, but when the player is reduced to 0 health, he gets stuck in that invisible and not colliding object. I am able to rotate, attack and do everything else, but the player itself cannot move.
Here’s the code for the water/terrain collider
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RespawnScript : MonoBehaviour
{
[SerializeField] private Transform player;
[SerializeField] private Transform respawnPoint;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
player.transform.position = respawnPoint.transform.position;
Physics.SyncTransforms();
}
}
}
And this is the code for the Player’s HP and its respawn point
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerHP : MonoBehaviour
{
[SerializeField] private Transform player;
[SerializeField] private Transform respawnPoint;
public int maxHealth = 100;
public int currentHealth;
public HealthBar healthBar;
void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.T))
{
TakeDamage(20);
}
if (currentHealth <= 0)
{
player.transform.position = respawnPoint.transform.position;
Physics.SyncTransforms();
}
}
void TakeDamage(int damage)
{
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
}
}
Thanks in advance