2
\$\begingroup\$

How can I move a new Vector3 position with a Transform? Like the transform is the parent the vector.

enter image description here

Example if I want to push the x position for example by 10 value and I rotate the transform its push in world coordinate but I want to push by the rotation too.

    void Update () {
    temp = transform.Find("Transform").position;
    temp = new Vector3(temp.x + 10.0f, temp.y, temp.z);

    Debug.DrawLine(transform.Find("Transform").position, temp);
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

This is what the transform.TransformPoint method does: takes a position in local coordinates and transforms it to world coordinates as though it were a child of the transform.

So you can do something like this:

// First, transform the point into a local offset.
Vector3 localSpacePoint;

public void SetFollowingPoint(Vector3 worldSpacePoint) {
     localSpacePoint = transform.InverseTransformPoint(worldSpacePoint);
}

// Then move your transform, and get the new position with:

public Vector3 GetFollowingPoint() {
     return transform.TransformPoint(localSpacePoint);
}

You'll notice there are also transform.[Inverse]TransformVector/Direction, too. These are for...

  • Direction: things that should be affected by rotation, but not scale or translation (eg. a heading direction)

  • Vector: things that should be affected by rotation and scale, but not by translation (eg. the relative offset between two objects that move together)

  • Point: things that should be affected by rotation, scale, and translation (eg. positions on your object, like attachment points)

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .