Home > Blockchain >  Get the global position of a child object in parent
Get the global position of a child object in parent

Time:06-29

I need to get the child's position in global value by using the parents rotation and the child x position. I've tried: planet.transform.localPosition This gives the position on the x axis but not where it is on the y.

        x = planet.transform.position.x * x;

        float y = Mathf.Sin(ring.transform.rotation.z);
        y = planet.transform.position.x * y;

        Vector2 position = new Vector3(x, y);

This doesn't get the proper position, it is off by quite a bit

transform.TransformPoint(planet.transform.position))

This did not work at all, it was off by far.

Then i tried instantiating an object and having it at 0,0,0 and have its rotation to the ring rotation and move transform.up to the planet position on the x.

Am i doing anything wrong here?

(The planets only use the X axis and rely on the parent "ring"'s rotation to change the position)

Diagram

The diagram on the left shows that the parent rotation is 0, and the child position is 0,10,0. The right diagram shows the parent's rotation is 45, but the child's position is unknown. What would it be and how would you work it out?

CodePudding user response:

The child's transform.position IS the global position. If you're trying to use the child's LOCAL x-position (i.e., relative to its parent) then you would want to use transform.localPosition.

But again, no need to step through any transform math, just grab the world position directly with transform.position.

:EDIT:

Here's a script I wrote. All it does is get the parent's rotation angles, if they exist, and show those along with the transform.localPosition and transform.position values.

using UnityEngine;

[ExecuteInEditMode]
public class Rotator : MonoBehaviour
{
    void Update()
    {
        string positionString = "";
        if (transform.parent != null)
        {
            positionString  = "Parent rotation: <"  
                              transform.parent.localEulerAngles.x   ", "  
                              transform.parent.localEulerAngles.y   ", "  
                              transform.parent.localEulerAngles.z   ">; ";
        }

        positionString  = "Child Local Position: <"  
                          transform.localPosition.x   ", "  
                          transform.localPosition.y   ", "  
                          transform.localPosition.z   ">; ";

        positionString  = "Child World Position: <"  
                          transform.position.x   ", "  
                          transform.position.y   ", "  
                          transform.position.z   ">; ";
        Debug.Log(positionString);
    }
}

Here's the output of the script. You can see the debug messages in the log there showing the rotation angles of the parent and the child's local position and the world position. I wiggle it by hand for a bit then I go through the 30, 45, 60, and 90 degree angles because those are common angles to memorize for trig functions: 0.5, sqrt(2)/2, sqrt(3)/2, etc. Note the radius (local position) is 5, so it's 5x those values.

If this still doesn't get you what you need then I need you to work through the math by hand for an example problem to show what exactly you're looking for that's different.

world_position

  • Related