1
\$\begingroup\$

Okay so here is the example

You have a plane at position 0,0,0 with a scale 15,1,15 (for demonstration)

Now you have a parent object for the sake of simplicity lets call it parentObject this parent object has the following position: 7,25,26 The parent DOES NOT have a mesh and is thereby an empty gameobject

The parent has a child gameobject Here the issue starts the child has a transform position at 5,-25,30 however this objects transform.position is relative to the parent

last take our agent i know set him to go to childObject.transform.position will he then move correctly to the object or will he, in fact, move to the position that is relative to the parent object?

I know this might seem a bit messy but the general question is if i tell an agent to go to a gameobject that is a child of another object using transform.position will he go to the place we would expect?

\$\endgroup\$
1
\$\begingroup\$

transform.position returns the position of an object in world-space coordinates, as in, where the object is in relation to the centre of the world. So basically, it "adds up" all transformations (including position) from all of its parents, to find out its final position.

transform.localPosition gives the local position of the object, as in, where the object is in relation to its parent (The value you see in the inspector).

So if my_obj.position returns (0, 0, 0) that means my_obj is located at the centre of the world. If my_obj.localPosition returns (0, 0, 0), then my_obj is located on the exact same location of its parent.

When moving objects around, if you use transform.position would give expected results, NavMeshAgents in Unity expect world-space coordinates, unless otherwise specified in the documentation.

\$\endgroup\$
  • \$\begingroup\$ Thank you so much for your reply. is there a better way to move your agents than transform.position?` \$\endgroup\$ – Marc Rasmussen Aug 31 '18 at 11:48
  • \$\begingroup\$ "Better" for what objective/goal/criteria, @MarcRasmussen? Is there a specific problem you've encountered with transform.position that you'd like strategies to overcome? \$\endgroup\$ – DMGregory Aug 31 '18 at 12:18
  • \$\begingroup\$ @MarcRasmussen If you want to move a NavMeshAgent you can do so using SetDestination(targetPosition) or some other method from its API. You can use transform.position to get the position of other objects to pass to the SetDestination. \$\endgroup\$ – TomTsagk Aug 31 '18 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.