Problem with object positions

I have two objects, a shelf and a trash. I put the trash next to the shelf with this code:

Vector3 shelf_pos = shelf.transform.position;
transform.position = new Vector3(shelf_pos.x - 1.75f, shelf_pos.y, shelf_pos.z - 0.62f);

And this puts the trash in the floor next to the shelf regardless the shelf’s position.

But doesnt work if I change the shelf’s angle.

Can someone help me please?

Usually to put something in a particular position relative to something other, you create amn empty placeholder object and make it the child object of that something other (shelf in your case) to denote desired object position relative to another object no matter how it’s placed in the world.

@palex-nx 's advice is solid. If you want a pure code solution, you can position the transform on the shelf’s local space:

transform.position = shelf.transform.TransformPoint(new Vector3(-1,75f, 0f, -0.62f));
1 Like

Works perfectly! Thanks