Teleporting player using transform.position only works sometimes

I am working on a game where after you beat a level the player gets moved to the new start position in the same scene using transform.position. In that same function all objects except the player get destroyed and re-instantiated into a bigger level (each level gets bigger as you move on). However, sometimes the player does not move from its position even though it receives the transform.position update. The only instance I’ve been able to avoid this bug is by keep the level the same size every time it gets re-instantiated. I also ran some Debug.Log calls and noticed that when this bug occurs the player does move to the new position but then snaps back to its old position in an instant. Here’s what that output looks like:

Current Position: (-126.7, 1.1, -127.3)
UnityEngine.Debug:Log(Object)
PlayerController:Update() (at Assets/Scripts/PlayerController.cs:276)

Current Position: (-126.7, 1.6, -127.3)
UnityEngine.Debug:Log(Object)
PlayerController:Update() (at Assets/Scripts/PlayerController.cs:276)

Sent: (200.0, 2.0, 200.0)
UnityEngine.Debug:Log(Object)
ProceduralGeneration:levelBuild() (at Assets/Scripts/ProceduralGeneration.cs:47)
ProceduralGeneration:Update() (at Assets/Scripts/ProceduralGeneration.cs:38)

Current Position: (200.0, 2.0, 200.0)
UnityEngine.Debug:Log(Object)
PlayerController:Update() (at Assets/Scripts/PlayerController.cs:276)

Current Position: (-126.7, 2.6, -127.3)
UnityEngine.Debug:Log(Object)
PlayerController:Update() (at Assets/Scripts/PlayerController.cs:276)

Also to give you a general idea this is what my transform.position update looks like:

script.transform.position = new Vector3((50*escalation)+100, 2, (50*escalation)+100);

As the game mode scales in difficulty so does the size of the level so the starting point gets farther and farther each time.

Any assistance with this issue would be greatly appreciated.

It looks like your player controller is overriding the position. I would guess you are using Unity’s character controller which is setting the position on the next Move call.

If you have a NavMesh, this could be a problem. You, have to begin the game with a navmesh bigger enough to cover the biggest level you could have.
WarmedxMints had another idea.

We need more informations, maybe a code.

Yes I am using Unity’s character controller but I am not using a NavMesh.
How would I go about preventing the Move call from being called called?

Thanks to WarmedxMints I was able to figure out a solution. It was connected to the use of the move call. All I had to do was add a check and delay to the Move call to prevent the player transform from being overwritten.

Here’s my solution in case anyone runs into a similar issue:

if(finished == false && respawnTime + tenthSec < Time.time)
        {
            characterController.Move(velocity * Time.deltaTime);
        }

finished is a bool which is set to true when the player collides with the finish area
this is also the same time respawnTime is given a time value
and tenthSec has a value of 0.1f

1 Like