Skip to main content
typo
Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

transform.up is a vector in world space, but transform.positionlocalPosition moves an object in its parent's space. If the parent (or grandparent, etc) is rotated, then these two spaces can differ.

To get the object's up vector relative to its parent coordinate system, just multiply:

Vector3 localUpInParentSpace = transform.localRotation * Vector3.up;

Then you can add it to your localPosition:

upwardShift = 1.0f; // Substitute whatever distance you want to move here.
Vector3 shiftedLocalPosition = transform.localPosition + localUpInParentSpace * upwardShift;

This moves along the object's local y+ axis. If you want to move along the object's local z+ axis, use Vector3.forward instead of Vector3.up.

transform.up is a vector in world space, but transform.position moves an object in its parent's space. If the parent (or grandparent, etc) is rotated, then these two spaces can differ.

To get the object's up vector relative to its parent coordinate system, just multiply:

Vector3 localUpInParentSpace = transform.localRotation * Vector3.up;

Then you can add it to your localPosition:

upwardShift = 1.0f; // Substitute whatever distance you want to move here.
Vector3 shiftedLocalPosition = transform.localPosition + localUpInParentSpace * upwardShift;

This moves along the object's local y+ axis. If you want to move along the object's local z+ axis, use Vector3.forward instead of Vector3.up.

transform.up is a vector in world space, but transform.localPosition moves an object in its parent's space. If the parent (or grandparent, etc) is rotated, then these two spaces can differ.

To get the object's up vector relative to its parent coordinate system, just multiply:

Vector3 localUpInParentSpace = transform.localRotation * Vector3.up;

Then you can add it to your localPosition:

upwardShift = 1.0f; // Substitute whatever distance you want to move here.
Vector3 shiftedLocalPosition = transform.localPosition + localUpInParentSpace * upwardShift;

This moves along the object's local y+ axis. If you want to move along the object's local z+ axis, use Vector3.forward instead of Vector3.up.

Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

transform.up is a vector in world space, but transform.position moves an object in its parent's space. If the parent (or grandparent, etc) is rotated, then these two spaces can differ.

To get the object's up vector relative to its parent coordinate system, just multiply:

Vector3 localUpInParentSpace = transform.localRotation * Vector3.up;

Then you can add it to your localPosition:

upwardShift = 1.0f; // Substitute whatever distance you want to move here.
Vector3 shiftedLocalPosition = transform.localPosition + localUpInParentSpace * upwardShift;

This moves along the object's local y+ axis. If you want to move along the object's local z+ axis, use Vector3.forward instead of Vector3.up.