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!

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.

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.