- Home /
Being spawned inaccurately on level load.
Hey so I've been working on a simple 2D platformer.
To make things simpler I created an empty game object called Spawn Point with a very small script to instantiate the player:
public GameObject player;
public GameObject spawnPoint;
void Start ()
{
Instantiate(player);
player.transform.position = spawnPoint.transform.position;
}
However, whenever the level restarts or a new level loads the character can appear quite a distance away from it. Usually about 2 or 3 "units" which can make the player start on the other side of the wall and not on the intended starting position... Like on top of a hole or on one of the items to collect.
I really have no idea why this could be happening, I even tried settings the position before instantiating (and didn't get any errors for the player not existing yet, funnily enough) but that still didn't work...
After resetting the level or dying (resetting the level therefore) it always instantiates correctly though...
Please let me know if you need more details, I really don't know what could be causing it and I don't want to tell you everything as it might be unrelated xD
Check this, maybe obvious: - The transform on the prefab is set to (0,0,0)
Did you try to instantiate giving the position? Instantiate (player, position, rotation);
Did you try to set the player with position Vector3.zero as a child of spawnPoint?
Anyway it seems a fight between local and world positioning, I would debug that.
Answer by Pixzle · Nov 29, 2015 at 10:54 AM
You should first check if the prefab position is set to 0,0,0. What your code is doing now is instantiating the player, then modifying the position of the actual prefab, not the instance you just created.
You could either do somethink like:
player.transform.position = spawnPoint.transform.position;
Instantiate(player);
this will edit the prefabs position before instantiating it.
You could also do something like:
public GameObject player;
GameObject instPlayer;
public GameObject spawnPoint;
void Start ()
{
instPlayer = Instantiate(player) as GameObject;
instPlayer.transform.position = spawnPoint.transform.position;
}
but easiest should simply be:
public GameObject player;
public GameObject spawnPoint;
void Start ()
{
Instantiate(player, spawnPoint.transform.position, spawnPoint.transform.rotation);
}
Code is untested, but it should work. I hope it helps!
Your latest solution appears to have worked properly :D
Perhaps it was an issue with rotation... since I am working on a 2D game the sprites wouldn't have rotated so it could have cause that...
Either way my prefabs were all set to (0,0,-1) with (0,0,0) rotation so not sure what happened x)
Cool to hear it works. What you did with your code was setting the prefabs position after instantiation, so next time you instantiate they get that offset.
player.transform.position = spawnPoint.transform.position;
only edits the PREFABS offset :D
oh I think I see what you mean now xD
The prefab is set to (0,0,0), so by instantiating it first it would appear on that position and only after would it change to the spawn position xD
But why then didn't it change position anyway?
Is it because the physics kicked in and changed the current position ins$$anonymous$$d of actually moving it to the spawn point?
Your answer
Follow this Question
Related Questions
how to "search" for a spawn point 0 Answers
Prevent object from spawning at a spawn point twice in a row? 3 Answers
[UNET] Dynamically position NetworkStartPosition Gameobjects 2 Answers
Spawn Point 2 Answers
Network spawning 1 Answer