1
\$\begingroup\$

So in my scene hierarchy, I have an object laid out like so; -Level1 --Level2 ---Level3 ----Level4

My script is attached to the Level1 object. When I try to find the transform.position of Level4 by way of Debug.Log to the console, I get the position of Level1, and so when I change the position of Level4 inside Level3, the data sent to the console doesn't change, still shows the position of Level1.

I do not believe this may be relevant, but what I am doing is an IK rig to move a character's limb. The object for which I am attempting to find the position is nested inside the skeleton of the character. The value returned to me is the position of the prefab placed in the scene. All of the objects (Level1 through Level4) are not explicitly moved, however they inherit their positions from the rotation of their parent.

My code to read the position is as follows:

Debug.Log(object.position);

Where object is already a transform. I have also tried:

Debug.Log(object.transform.position);

But still, all I get is the position of the top level parent.

Thanks for any help.

ADDITION

Ok so to expand on the question, I'll explain exactly what I'm trying to do. I have the mecanim example and unity 5.0.1f1. I have the Mecanim Example project from the asset store and I'm in the animator controller scene. I have enabled IK pass on my base layer, and have placed my code in the "idle run jump" script, attached to the top level of the game object for the playable character. I have then attached an empty game object inside the left foot to mark the location of where the left heel meets the floor. This object is attached in the inspector to a variable in the "idle run jump" script, which then receives the ik pass through the function OnAnimatorIK. My aim is to get the foot to follow the same position as another object, where leftFoot follows leftFootObj. My code is as follows;

using UnityEngine;
using System.Collections;

public class pos : MonoBehaviour {

    protected Animator animator;
    public float offset = 0.156f;

    public Transform leftFoot;
    public Transform leftFootObj;

    void OnAnimatorIK() {
        if (animator == null)
            return;

        if (leftFoot != null) {
            Debug.Log(leftFoot.position);
            animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot,1);
            animator.SetIKRotationWeight(AvatarIKGoal.LeftFoot,1);
            animator.SetIKPosition(AvatarIKGoal.LeftFoot,(leftFootObj.position+(Vector3.up*offset)));
            animator.SetIKRotation(AvatarIKGoal.LeftFoot,leftFootObj.rotation);
        }        
    }

    void Start () 
    {
        animator = GetComponent<Animator>();

        if(animator.layerCount >= 2)
            animator.SetLayerWeight(1, 1);
    }

}
\$\endgroup\$
  • \$\begingroup\$ Your question is a bit ambiguous, Debug.Log(object.position); is attached to the nested Lvl4 object I presume? did you try Debug.Log(this.transform.position); \$\endgroup\$ – Zee Jun 18 '15 at 11:25
  • \$\begingroup\$ Thanks for the reply. The script in question is attached to the level1 object, and so I have created a script into the level4 object, and done a Debug.Log of it's position, which returns the exact same information. I am wondering whether there may be a problem due to the fact that the position of the level 4 object is affected by the rotation of the level2 and level3 objects? \$\endgroup\$ – Michael Thompson Jun 18 '15 at 11:58
  • \$\begingroup\$ Add the exact code you are using, without it your question will be almost impossible to troubleshoot. \$\endgroup\$ – Jeremiah Leslie Jun 18 '15 at 15:49
  • \$\begingroup\$ Sorry, forgot to comment. I added the information you requested. \$\endgroup\$ – Michael Thompson Jun 19 '15 at 6:39
  • \$\begingroup\$ Bump. Don't suppose anyone's got any more suggestions? \$\endgroup\$ – Michael Thompson Jun 21 '15 at 13:28
1
\$\begingroup\$

The position of level4 is dependent of all its parents, if you want to attach your script to the parent level1, you can call the first child of a gameObject by :

this.transform.GetChild(0).transform.GetChid(0);

This will address the first child of the child of the gameObject attached to the script, if you want to go deeper, just repeat the function. If you would like to call the child directly by Name instead of using this method, just use :

GameObject.Find(Level1/level2/level3/level4);

and then address its transform, or whatever you need from it.

\$\endgroup\$
  • \$\begingroup\$ I've already targeted my object, which I have placed at the top of my script to show in the inspector. I have verified I have selected the correct game object. I already have the transform I need to address, however when I look at the position in my script, it gives me an unexpected position value, regardless of where the level 4 game object actually is in the 3d world. \$\endgroup\$ – Michael Thompson Jun 18 '15 at 12:11
  • \$\begingroup\$ Remember, the level4 object's position is relative to his parent, if the parent is in xy(1,1) and the child is in xy(1,1) coordinates, then the childs coordinates are xy(2,2) in the world. \$\endgroup\$ – Zee Jun 18 '15 at 19:24
  • \$\begingroup\$ Yes I'm aware of that but that's not the problem. The object is attached to a limb on a character. When requesting the position of the limb (not local position), it returns the value of the parent's position which is different to the position of the object for which I wish to request the position \$\endgroup\$ – Michael Thompson Jun 19 '15 at 2:22
0
\$\begingroup\$

Despite my late reply this answer might be the solution that some people are looking for. It might save you hours of debugging as it would've saved me quite a lot if I found an answer like this. The answer is bad pivot points.

I had the same issue where the nested childs of a gameObject had the same position as the top level parent. In Unity the pivot point is default shown at the center of the gameObject. However this might not be the case in the actual model! Simply by toggling the tool handle position from Center to Pivot should show if pivot points are the issue here (which it was in my case).

The artist on my end had simply detached objects to create children, however the pivot point was not reset to be a zero position of the new mesh (child). It was still the same point as that of the previous mesh (parent). This resulted in the pivot points of nested children being the same as the parent.

If you encounter the same problem, triple check the pivot points in your modelling software before starting countless hours of debugging trying to figure out what is resulting the "fake" position... while the pivot simply isn't in expected position.

\$\endgroup\$
  • \$\begingroup\$ Note that you can toggle where Unity displays the gizmo of an object, between the center of the object and its pivot (local 0,0,0), so it's not "always" shown at the center. See the Gizmo Toggles described here \$\endgroup\$ – DMGregory Aug 1 '18 at 2:20
  • \$\begingroup\$ I wasn't aware of this, thanks for the heads up! I have edited my answer. \$\endgroup\$ – Ravi Bechoe Aug 1 '18 at 2:27

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.