- Home /
How can I set the default position of a 3D object?
So my problem would be that I am making a 3D Shogi game and I am all new to unity, I just downloaded it 2 days ago, and I cannot find out how to make my pieces on the correct position on the board when I start with the play button. I already have a script which spawns 20 pieces what I need, but these are all under the board. I have to make them on the correct places, I would appreciate some help a lot.
Answer by unity_ek98vnTRplGj8Q · Feb 12, 2020 at 05:08 AM
The quick and easy way to do it would be to spawn 1 piece in, move it so that it rests on top of the board, then read the Y value of its position in the inspector and set it explicitly as part of the MovePiece function.
p.transform.position.y = //put your calculated y value here
However hard coding these kind of offsets is bad practice and can only lead to headaches down the road. A "better" way may be to make an empty child object for the board and position it so that it rests on top of the board. Then you can get a reference to it and read its position at runtime, so that even if you move your board later the pieces will still spawn in the same spot
//When declaring class variables
//Drag your empty game object into this field in the inspector
public Transform referenceObject;
//Down in your actual code
p.transform.position.y = referenceObject.position.y;
If you choose the second method, make sure that the origin of each piece is at the base of the piece. If its not, you will either need to add an offset based off of where the origin is for the piece or change the pieces so that the origin is at the base
Thank you so much for your answer, it is working now, thanks to you!
Your answer
Follow this Question
Related Questions
Destroying projectile on collision with every object. 1 Answer
Fade the start and the end of a moving Image 1 Answer
problem with joints 0 Answers
Need Help Limiting Rotation on Object's X axis 1 Answer
(New UI) 3D objects as buttons? 1 Answer