- Home /
Gameobject placing halfway through ground?
I'm making an RTS and I have a GUI that allows me to click a building I want to place and it copys it so I can place it somewhere on the ground but whenever I click to place the building it is halfway in the ground already (See picture). What is causing this to happen?
if (Physics.Raycast (ray, out hit, Mathf.Infinity, Ground)) {
currentBuilding.transform.position = new Vector3 (hit.point.x, 0f, hit.point.z);
//currentBuilding.transform.position = hit.point;
//Both ways make the building go through the ground...
}
[1]: /storage/temp/84180-pic.png
Answer by clyburnw · Dec 16, 2016 at 01:43 AM
The origin/pivot of your model is in its center, not at its bottom. You're setting its Y to 0, which is where your ground plane is. So, anything in the model below its origin will be below 0, which is below your ground plane.
Pause in play mode, and look at the model in the scene view.
Is there a simple way to change the origin/pivot? Thanks for the info
Cleanest way is to just load it up in a 3d modeler and fix the pivot directly. Quick way is to parent it to another gameobject, which could be empty, and set the model's transform relative to the parent. Then, you'll set the position on the parent to 0, ins$$anonymous$$d of the model's gameobject. Eg., if your model is 5 units below the ground at the origin of the scene, then the Parent would be at (0,0,0) and the child model would be at (0,5,0) relative to the parent. When you move the parent, the child comes along.
Your answer
Follow this Question
Related Questions
How to Place a Game Object in game(Place a building) 0 Answers
How to make a circle under a gameobject being chosen in a RTS game 1 Answer
Grid system visualization 1 Answer
RTS Building Creation System Code not working (no errors) 2 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers