Instantiating an object via its center (as opposed to its pivot point)

Hello,

I am trying to instantiate a prefab object in front of the player, so they can build a wall.

When they place the wall, it needs to be central to the camera (placed via its center).

Unfortunately, the wall’s pivot point is used to instantiate it. This means it is placed to far down and too far to the left.

I need to instantiate an object by its center, I can think of two ways to do this:

a) Place the object by its center somehow? Maybe there is some kind of option with the instantiate() function?

b) Change the pivot point to be the center of the object, I have no idea how to do this.

Just in case, I have tried changing the “pivot” button in the top left to center - it doesn’t work and I can’t see how it would help changing it :S

Thanks in advance,

~Joe

PS: Sorry for the stupid tag. Unity Answers is being stupidly buggy, suggested tags or not loading, the tag page isn’t loading and “object” was the only tag I could find that existed - if tags are working for you please edit this!

If it is not .fbx you can put it into a .zip file and post it here, I will then adjust the pivot point for you. The "pivot" point in the menu just changes where the editor transforms are in relation to the object. You could also write a vector offset in your placement script to sort it but that is a worse option that changing the pivot point itself. Anyway post it here if you can and I will fix it for you, Blender won't import .fbx though so I'm stuck if that is all you can give me!

If you want to move it, try using using the [SetPivot editor script from the Unity Wiki.][1] [1]: http://wiki.unity3d.com/index.php?title=SetPivot

MrSoad: Thanks for the offer, but I'd rather learn how to do it than get somebody else to :P Could you point me in the right direction of changing the pivot point in Blender please? I'd love to learn how to. roberbu: I'll try that in a few hours when my Unity actually starts up...

2 Answers

2

To get the center of a GameObject use:

gameObject.renderer.bounds.center

This will return the center point (x,y,z) of the GameObject in the global coordinates.

Probably not the best solution to his instantiate problem, this is adding unnecessary code etc, prob much better to move the anchor and cut all this sort of stuff out.

Moving the anchor means editing the gameObject. Which is something I'm not sure he can do (from the content of the question). Alternatively, creating a prefab where the GameObject is the child of an empty GameObject at its center, can also solve the problem, without adding any code.

If you read above I offered to do this for him, it's a two second Blender job which I don't mind doing for anyone. Yes your idea of having it in an empty to auto offset is a good one, one which I have used repeatedly to sort out the difference between the way Unity and Blender set their x,y,z axis.

FGP buried this in a comment, above, but it’s the simplest and most common answer:

Give the object a parent, and center it there. Many, many Answers here explaining it – try “unity parent recenter”. It’s like a top-10 Unity trick (and I think just modelling in general.)

The other method, which you named (a), involves coordinate math. Say you have the local center, maybe from an empty child, can subtract that (if the center is +1y, spawn the object -1y.) It can get messy when you have rotated objects, since you have to convert from local to world space. Only useful if you need the pivot point different from the center.

Your wall prefab sounds like it's using the "parent" trick already. For example, you could move the bricks around in the wall prefab parent so the pivot/placePoint is lower center, middle, lower left ... . If you really, really want, you could also use the Mesh class to directly move the verts (which changes the pivot,) but that's almost never worth it.