Not able to set transform.position. Player keeps snapping back to position before change.

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Not able to set transform.position. Player keeps snapping back to position before change.

Gnat
2021-09-03 00:50:50

Hi there! this is my first post here, I am having trouble moving my players to a new position when I change from scene A to scene B..
I will try my best to describe my current setup..

I have a SceneSetup script in "scene B" which contains a list of spawnpoints.(GameObjects)
I also have a PlayerManager script which is instantiated in "scene A" and set to DontDestroyOnLoad. it also has a list of "currentSpawnPoints"
my Players are also set to DontDestroyOnLoad.

when I change from scene A to B.
In Awake my SceneSetup finds the PlayerManager and populates the currentSpawnPoints List with all of the spawnpoints for that scene...

it then calls a function on the playerManager which will loop over all of the players in the scene. and set the player[i] position to spawnpoint[i] position

The issue is that my player will get set to that position for 1 frame and snap back to its previous position instantly..
I have tried calling the function in update and they get set alright (but obviously setting it every frame means the player cannot move from that position).
I have tried setting this up with RPC's and RaiseEvents however the same problems occurs...
I beleive the problems is that the position is being set.. and the PhotonTransformView sets the resyncs the position after my code is called.
i have popped debugs all through the code, and everything is being called correctly..

Here is snippets of my code, Any and all help is greatly appreciated!! :)

SceneSetup.cs
void Awake()
{
Debug.Log("SceneSetup: Awake");
playerManager = GameObject.FindGameObjectWithTag("PlayerManager").GetComponent();
playerManager.AddSpawnpoints(spawnPoints);
playerManager.SetPlayerPositions();

}  

PlayerManager.cs

public void AddSpawnpoints(List<GameObject> spawnpoints)  
{  
    Debug.Log("MANAGER: AddSpawnPoint");  
    currentSpawnPoints.Clear();  
    for (int i = 0; i < spawnpoints.Count; i++)  
    {  
        currentSpawnPoints.Add(spawnpoints[i]);  
    }  
}  

public void SetPlayerPositions() { Debug.Log("MANAGER: SetPlayerPosition"); for (int i = 0; i < players.Count; i++) { players[i].GetComponent().SetPlayerPosition(currentSpawnPoints[i].transform.position); } }

PlayerController.cs

public void SetPlayerPosition(Vector3 pos)   
{  
    Debug.Log("PLAYERCONTROLLER: SetPlayerPosition" + _spawnPoint);  
    RPC_SetPlayerPosition(pos);  
}


[PunRPC]  
public void RPC_SetPlayerPosition( Vector3 pos)   
{  
    Debug.Log("RPC_PLAYERCONTROLLER: SetPlayerPosition");  
    transform.position = pos;  
}

Comments

Gnat
2021-09-03 01:47:50

I have found a workaround... although it seems abit sketchy....
I am disabling the character controller before setting the players transform
and then re-enabling it afterwards...

public void SetPlayerPosition(Vector3 pos)
{
cc.enabled = false;
transform.position = pos;
cc.enabled = true;
}

Gnat
2021-09-03 01:50:00

if anybody has any ideas as to why this happens.. or possibly a better way to move players position it would be a great help :)
Thanks for creating such a powerful tool to create multiplayer games :)

Back to top