- Home /
Positioning Prefabs in exact positions
HI all, Im at my wits end here. Im sure its something simple that Im missing. Any help would be SUPER appreciated.
So I have a prefab,its called wall.It consists of an empty parent object, a super basic 3d mesh(imported from max) which is a wall section and a box collider that covers the wall mesh. What I want is to instantiate 4 of these in a square formation. The idea being to generate a few of these to build a tunnel.The problem is positioning the prefabs.They just dont fit together. The prefabs overlap. One odd thing, the scale of the mesh in the inspector is 0.0254(x scale for example),but the x scale of the collider is something like 59. Also the z offset of the collider centre is non zero for some reason,if I set it to zero then it doesnt cover the mesh . Here is the code I use to instantiate:
float wallHeight = wall.transform.Find("Wall Side").GetComponent<BoxCollider>().size.y;
float wallLength = wall.transform.Find("Wall Side").GetComponent<BoxCollider>().size.x *
wall.transform.Find("Wall Side").localScale.x;
Debug.Log("loopSidelength : " + wallLength);
Vector3 p = new Vector3(0,0,0);
//RHS
p.x = centrePoint.x + wallLength/2f;
p.y = centrePoint.y;
p.z = centrePoint.z;
GameObject right = (Instantiate(wall,p,Quaternion.Euler (0,270,0)) as GameObject);
right.name ="Right";
//LHS
p.x = centrePoint.x - wallLength/2f;
p.y = centrePoint.y;
p.z = centrePoint.z;
GameObject left = (Instantiate(wall,p,Quaternion.Euler (0,90,0)) as GameObject);
left.name = "Left";
//Top
p.x = centrePoint.x;
p.y = centrePoint.y;
p.z = centrePoint.z + wallLength/2f;
GameObject top = (Instantiate(wall,p,Quaternion.Euler (0,180,0)) as GameObject);
top.name = "Top";
//Bottom
p.x = centrePoint.x;
p.y = centrePoint.y;
p.z = centrePoint.z - wallLength/2f
;
GameObject bottom = (Instantiate(wall,p,Quaternion.Euler (0,0,0)) as GameObject);
bottom.name ="Bottom";
Answer by incorrect · Jun 20, 2014 at 11:32 AM
That is because your model made that way. It's center point does not match with center of collider (that is why z offset is not zero) and it's scale does not match collider's scale because your collider is probably something like 1 point x 1 point x 1 point in dimensions and model has different size. You should google how to set model's pivot point and scale and reimport it so it will match scale in Unity and it's center point will satisfy your needs. Also you can create empty GameObject and make your model it's child, positioning this proxy GameObject to the point you want to be center.
Cheers, yeah I removed the model and used a unity primitive ins$$anonymous$$d and all worked fine. Ill build using the primitives for now and revisit the models at a later date. Thanks!!
Your answer
Follow this Question
Related Questions
GameObject position and localPosition not changing in hiearchy, only in script. 0 Answers
BCE0019: 'position' is not a member of 'Object' 0 Answers
How I can update prefab properties using c# 1 Answer
Lerp to position in instantiated prefab? 1 Answer
How to set the UI Text position to be relative to his "grandfather" prefab 0 Answers