Change starting position of Instantiate object
Hello,
I am new to unity, so there might be some mistakes in my logic but all ideas are welcome.
I am developing a 2D game multiplayer game.
I have to say that the screen is scrollable and only a part of the scene is visible on the camera every time. In every game for the first player I want the sprites to move from left to right, and for the second from right to left. So I use this code to flip the whole world
void Flip()
{
//Get the current state of world to a temp variable
Vector3 theScale = transform.localScale;
//Reverse the vector
theScale.x *= -1;
//Set the vector as the new state of world
transform.localScale = theScale;
//Basically reverse the whole world including the sprite
}
I have the original sprite created and it starts and moves normally from left to right or from right to left according to the player's turn using this code
startingCollider = redStartingCollider; // collider on left side of screen, same for player 1 and 2 because of world fliping
//To set a default position
Vector3 pos = transform.position;
pos.x = startingCollider.transform.position.x;
transform.position = pos;
if (PlayerScript.playerTurn == 2)
{
speed *= -1;
}
Now I want the user to create new sprites when clicking on a button and I want sprites to begin from the same position every time. So I use this code when the button is pressed
GameObject ob=Instantiate(unit);
ob.transform.parent = parentObject.transform;
// To set default position not a relative to an object
Vector3 pos3 = ob.transform.position;
if (PlayerScript.playerTurn == 1)
{
pos3.x = redStartingCollider.transform.position.x; //collider on left side of screen
}
else
{
pos3.x = blueStartingCollider.transform.position.x; //collider on right side of screen
}
pos3.y = 13;
ob.transform.position = pos3;
If the player's turn is 1 and there is no flip in the world the orignal sprite and it's children begin to move correctly from left to right.
The problem is when player's turn is 2 and the world flips. The original sprite begins from right and moves to left but it's children continues to begin from left.
What am I doing wrong? Any suggestions? Thanks in advance and sorry for the long post!
PS. Colliders that are used as starting points are not necessarily visible on the screen every time. I just want sprites to start from this specific point every time one is created.
Try using transform.localPosition ins$$anonymous$$d of transform.position, since the ob GameObject is a child of parentObject.
I tried it, yet nothing changed. The copies still start from left. If they are copies shouldn't they inherit the world settings from their parent? Shouldn't they behave the same way?
Answer by nansy_g21 · Oct 01, 2015 at 03:52 PM
Ok I figured out a solution not probably not the best one but it works at least for the moment. So for anyone with this kind of problem, I gave up the idea of flipping the whole world round. I just flipped the sprite using this code for the original sprite
if (PlayerScript.playerTurn == 1)
{
startingCollider = redStartingCollider;
}
else
{
startingCollider = blueStartingCollider;
speed = -5;
transform.Rotate(new Vector3(0, 180, 0));
}
And this code for the children
if(PlayerScript.playerTurn==1)
{
startingCollider = redStartingCollider;
}
else
{
startingCollider = blueStartingCollider;
Vector3 pos2 = startingCollider.transform.position;
pos2.x = pos2.x - 5;
startingCollider.transform.position=pos2;
}
// Create a game object reference of the copy to transform its parent
// We use 2 cameras in the game we have to put it in the right one
GameObject ob=(GameObject) Instantiate(unit,new Vector3(startingCollider.transform.position.x,13,0),unit.transform.rotation);
ob.transform.parent = parentObject.transform;
if(PlayerScript.playerTurn==2)
ob.transform.Rotate(new Vector3(0, 180, 0));
There are some more "ifs" in it though. So obviously there are better solutions