- Home /
How to find the relative position of a side of an object
I have a cube and I would like to use addforceatposition and apply this position to the center of a particular cube face in order to make that face turn to face the pulling object.
I do not understand however how to calculate the position of the cubes side for the position attribute of the function. Is this suppose to be a relative position, or am I suppose to get the center of the cube from its position, and then add 1/2 its dimensions to that point to get the particular side while accounting for rotation? Does Unity have some means of calculting the position of that side relatively and then converting it to world coordinates?
Thank you for your help.
Answer by aldonaletto · Nov 02, 2011 at 01:58 AM
A simple way to find the center of a cube face is to use the local references transform.up, right and forward. They are unit vectors, thus you can multiply them by the cube size / 2 and add to the cube position, as you suspected. For instance:
front face center: transform.position + transform.forward * cubeSize/2;
back face center: trasnform.position - transform.forward * cubeSize/2;
left face center: transform.position - transform.right * cubeSize/2;
bottom face center: transform.position - transform.up * cubeSize/2;
and so on. You don't have to worry about rotation, since these local vectors follow the cube orientation.
Doesn't this assume the objects pivot/origin is in the center of the object?
Yes, and that's true for cube primitives in Unity. I suspect that Unity sets the pivot of any mesh in the middle of the $$anonymous$$ and max points. Let's suppose that a given mesh has $$anonymous$$ = (-1,0,0) and max = (1,1,0.6): its pivot should then be at ($$anonymous$$+max)/2, what means (0.0,0.5,0.3)
As @Bunny83 said below, the pivot is always at the model origin - I've got confused by the Pivot/Center button in the Editor, which was set to Center (shame on me!).
No, Unity doesn't "place" the pivot anywhere. The pivot is not a "special location". It's always (0,0,0) in local space. It's simply the origin of the objects local space. $$anonymous$$ost modelling tools have a "move pivot" function. However what actually happens when you move the pivot from the center to the bottom is that all vertices are actually moved upwards. The vertices are defined in localspace.
The default cube primitive simply has it's vertices defined around the origin. Though custom meshes could have it's origin anywhere. For human models it's quite common to place it at the bottom.
Note only that but also the cube need to be aligned with the local coordinate system. This is usually the case for cubes, but it's possible to define the cube so it stands on a corner when not rotated. In this case the local axes wouldn't match with the cube faces.
The reason I ask is because I've just written some routines for aligning and snapping objects together that are from arbitrary sources like $$anonymous$$agicaVoxel, Blender, VoxelShop and they have to take into account that the origin is often not in the center or 0,0,0 (local).
Your answer
Follow this Question
Related Questions
Problem stopping rigid body after adding a force 0 Answers
Making something move relative to something else, but slower/shorter ? 2 Answers
relative velocity of rigidbody according to rotation... 1 Answer
Prevent Rigidbody from Climbing Steep Slopes using Forces (C#) 0 Answers
Objects continue to have force applied after escaping force range 1 Answer