Hello all,
I have a script for respawning my player after death. In the script I get the nearest “respawn point” and set this position to my player. All of that works fine and if I log the new position of my player it is the same as the respawn point, but in my scene the player doesn´t move/teleports to this position. Could there anything be that blocks the “teleport” ?
Each respawning script I saw, do the same like mine. To be sure here is my script:
GameObject _player;
PlayerCharacter playerCharacter;
public void Respawn()
{
_player = GameObject.FindGameObjectWithTag("Player");
GameObject[] respawnPoints = GameObject.FindGameObjectsWithTag(GlobalConstants.RespawnPoint);
Transform nearestRespawn = GetNearestRespawnPoint(respawnPoints);
PlayerCharacter playerCharacter = (PlayerCharacter)_player.GetComponent(GlobalConstants.PlayerCharacter);
_player.transform.position = nearestRespawn.position;
playerCharacter.IsDead = false;
}
private Transform GetNearestRespawnPoint(GameObject[] respawnPoints)
{
Transform nearestRespawnPoint = null;
float closestDistanceSqr = Mathf.Infinity;
Vector3 currentPosition = _player.transform.position;
foreach (GameObject respawnPoint in respawnPoints)
{
Vector3 directionToRespawnPoint = respawnPoint.transform.position - currentPosition;
float dSqrToRespawnPoint = directionToRespawnPoint.sqrMagnitude;
if (dSqrToRespawnPoint < closestDistanceSqr)
{
closestDistanceSqr = dSqrToRespawnPoint;
nearestRespawnPoint = respawnPoint.transform;
}
}
return nearestRespawnPoint;
}
#Edit:
My player has a Character Controller, Rigidbody and a Sphere Collider can anything of that prevent that it works correctly ?
Kind regards