How to transform from worldspace to localspace and back with new math library?

Hi,

I need to convert some old code, but i cannot find the right methods in the new math library :smile:

Can you guys help me?

var localMovement= transform.InverseTransformDirection(_rigidbody.velocity);
//[...] some transformation of localMovement
_rigidbody.velocity = transform.TransformDirection(localMovement);

:smile:

This should do it:

var localMovement= math.rotate(math.inverse(transform.rotation), _rigidbody.velocity);
//[...] some transformation of localMovement
_rigidbody.velocity = math.rotate(transform.rotation, localMovement);

The important thing here is that Unity.Mathematics.math works with Unity.Mathematics.quaternion, which you will be using a lot. Unity.Mathematics.quaternion has some quirks, though. Such as .Euler requiring Radians instead of Degrees.

2 Likes

You are a lifesaver, this works flawlessly :smile::slight_smile:

1 Like

if you want to use Transform Functions like normal GO workflow, the Matrix4x4 contains many of them.

1 Like