- Home /
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.
$$anonymous$$rSoad: 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...
Ok, you don't need to have any ability at modelling to adjust the pivot point in Blender so :
1) Open your model in Blender.
2) Enter Edit $$anonymous$$ode
3) Select your whole mesh (press A until it is completely highlighted orange)
4) $$anonymous$$ove the mesh around until it is in the right position in relation to the pivot point(also know as the anchor, or in Blender the origin).
5) Save you file
Now it should be in the right place when in Unity, I would only recommend code as a last resort when there is a reason why you cannot do it either this way or by using the parent object as an adjustment. With pivot points I always adjust them this way, with axis problems I always adjust via the parent method.
Answer by FairGamesProductions · Nov 08, 2014 at 01:08 AM
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.
$$anonymous$$oving 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 $$anonymous$$d 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.
Thanks for the answer(s)!
Unity is broken on my computer right now (it's not starting up ETC), so I won't be able to to test this until I have it working again (a couple of hours).
If you're able to set the center, then I think my problem is solved. Thanks! (If it does work :P)
$$anonymous$$rSoad: I don't $$anonymous$$d extra code - I would rather do things in a way which will always work. If it's a "better" way to do it in code, it's a good idea to do it now (I think).
FairGamesProductions: I am certainly a learner, I've been using Unity on and off for around 2-3 years now but I keep jumping into things head first. If I need to learn how to use another program (say Blender for example), I'll go and do it! However I always prefer to do it the "Unity way", and I'm not sure if using Blender is the best way to do it?
In this case just add this one line after your Instantiate:
GameObject clone = (GameObject)Instantiate(yourPrefab, targetPosition, targetRotation);
clone.transform.position += clone.transform.position - gameObject.renderer.bounds.center;
And it will be placed at the center of the objects renderer. This of course only works for single object. If you group several objects you would need to combine all the bounds to one. Something like:
var clone = (GameObject)Instantiate(yourPrefab, targetPosition, targetRotation);
Bounds B = clone.GetComponentsInChildren<Renderer>().Select(r => r.bounds).Aggregate((b1, b2) => { b1.Encapsulate(b2); return b1; });
clone.transform.position += clone.transform.position - B.center;
Note: Both methods will fail if there is no renderer attached. For the second solution you need a using System.Linq
at the top.
Answer by Owen-Reynolds · Nov 08, 2014 at 05:44 AM
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.
Thanks for the answer!
Unfortunately that is the only way I have seen to do it - which is rather stupid.
I'm not good at modeling, I don't like it because I when I model stuff it doesn't look how I imagined it, ins$$anonymous$$d, it looks like a mutant penguin managed to get hold of my keyboard.
For that reason, I like simple stuff (with a lot of physics) - my wall prefab is literally a bunch of blocks (bricks) put together in the shape of a wall. Everything is done inside Unity.
It seems stupid there is no built in way to adjust a pivot point, is there anyway I can somehow submit this as a requested feature (or see if it is currently being worked on ETC - I have never really wanted/needed a new feature in Unity before).
I'll try the parenting method and have a look at how other people have managed to achieve perfect centers using it - that might be the best option without a third party program :(
Thanks again :D
@ducsuus: You have to understand that the pivot point is not just a point that is set somewhere. It's actually the other way round. All vertices of your mesh are relative to your local 0,0,0 and that's your pivot point. "moving the pivot" means you would move all vertices in the other direction. The pivot is a property of the model data itself. Unity isn't a modelling program so it's very unlikely that they will include a method to edit the mesh and move the pivot point.
In most cases parenting is the easiest solution. I always parent the "model" to an empty gameobject. That allows you to even replace the model without affecting the "logical" parent object which actually holds scripts, rigidbody ...
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 $$anonymous$$esh class to directly move the verts (which changes the pivot,) but that's almost never worth it.