How to find out local coordinates relative to the parent without adding an object to it? Unity

47 Views Asked by At

I'm adding an object to the parent:

Transform target = Instantiate(_leftTargetPrefab, leftTargetPosition, Quaternion.identity);
target.SetParent(_ballsParent, false);

I'm trying to get the coordinates inside the parent, but they don't match:

Vector3 testPos = _ballsParent.InverseTransformPoint(leftTargetPosition);

Is it possible to find out the coordinates inside the parent without adding an object?

2

There are 2 best solutions below

0
The Ash Bot On

To get the coordinates relative to the parent, you can subtract the coordinates by the parent's world position.

Vector3 relativeCoordinates = coordinates - transform.parent.position;

To get the position of a child relative to the parent you can use transform.localPosition instead of transform.position

0
Soul On

I am not sure if i get you right.But if you are looking for a position of a child object try this:

var localPosition = _ballsParent.transform.Find("ChildName").transform.localPosition;

//Btw, the answer from "The Ash Bot" would be the better practice than find gameobject by the name :P