[Solved] GameObject position doesn´t change in scene

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

This is most likely due to the fact that the player’s character controller is a physics object, and that means there could be issues due to the new way physics are handled. (Not updated until after the frame is complete). In other words, what’s probably happening is that you’re setting the position of the player, but somewhere in your code later on in the frame it’s moving the controller with Move().

A quick way to fix this is to force a physics update after you force set the position of the character:

_player.transform.position = nearestRespawn.position;
Physics.SyncTransforms();

Note: this solution is not ideal - don’t do this too often. A new(ish) physics change made it so physics are only synced at the end of the frame, which is a lot faster than syncing every time an object moves during a frame. Physics.SyncTransforms() forces a sync, so if you do this every frame you’d be syncing twice a frame (or more). It’s great if you need to do something like teleport the player somewhere though, since that’s a rare thing to do.

3 Likes

Thank you very much. Your code did it!
I checked where I use the controller but actually I only have it in my movement script and I don´t use the Move() method (I´m a beginner with Unity/Game development, so maybe anything in the background is using it).
But atm I don´t have any trouble with that so it´s ok. Thx again!

1 Like