I have a new problem with relocating objects that’s baffling me. I’ve built a teleporter object (something I’ve done successfully before), but for some reason it’s behaving really strangely. The teleporter is just basically a trigger box collider.
I have a Player object and three standard Unity spheres in my scene named SP1, SP2, SP3 (spawn points).
When the player hits the teleporter, I’ve hardcoded this to happen (in reality, the spawn point will be variable):
Player playerObject = GameObject.Find("Player").GetComponent("Player") as Player;
playerObject.PlacePlayer(GameObject.Find("SP3").transform.position);
PlacePlayer just does this for now:
public void PlacePlayer(Vector3 NewPlayerPosition)
{
gameObject.transform.position = NewPlayerPosition;
}
But, everytime, the player hits the teleporter, they are transported to the object named SP2 instead of the object named SP3.
Does anyone have any idea what could be happening? It’s almost literally blowing my mind
There’s definitely something weird going on. I just tried simplifying things by having the teleporter’s OnTriggerEnter just execute the following directly:
Both of the renderer.enabled statements work (I have the renderers disabled at Start), but using this code, the player object just doesn’t move at all. That last line of code seems to be completely ignored.
OK. Figured this out. What I wasn’t taking into account was my movement code that’s still executing at the time OnTriggerEnter fires. The player is lerping forward, overwriting its transform.position, so technically the player WAS being teleported but only for maybe a frame. The next movement interation was then putting it back where it was in the first place (at SP2), so you couldn’t really detect that it was happening at all.
The teleporter plays audio when the player hits it. I just hadn’t assigned the audio yet.
I changed the script to play the audio, wait audioclip.length, and THEN teleport the player. The wait is long enough that the player’s movement completes and everything goes smoothly.
Because startPosition and endPosition are stored prior to the OnTriggerEnter “overriding” them, the movement continues on unhindered in it’s next iteration, overwriting the change that the OnTriggerEnter script just applied.