Custom LookAt Function Help

Hello!

I’m trying to get a quaternion (or Euler, whichever is easiest / most performant) from 2 points in space, where the right component(red gizmo arrow) of the rotation is the direction of one point towards another with the forward component (blue gizmo arrow) facing upwards: where the up component (green gizmo arrow) is perpendicular to a flat plane. Essentially the result of a transform.LookAt() function where forward is right and up is forward.

I’m using ECS (float3, lower-case quaternion), so I can’t use external functions. I’ve included an image to hopefully make a little more clear what I’m trying to achieve.

Thanks!

9579037--1356118--Screenshot 2024-01-13 at 2.40.23 AM.png

Probably just need Quaternion.LookRotation: Unity - Scripting API: Quaternion.LookRotation

You can specify an up-direction for the rotation.

1 Like

Here are the ECS equivalents to the above method:

https://docs.unity3d.com/Packages/com.unity.mathematics@1.3/api/Unity.Mathematics.quaternion.LookRotation.html
https://docs.unity3d.com/Packages/com.unity.mathematics@1.3/api/Unity.Mathematics.quaternion.LookRotationSafe.html

The problem with LookRotation, is the forward vector is always the “z” component (the blue gizmo arrow on the object points towards the target), while what I am trying to achieve is have the “x” component (red gizmo arrow) point towards the target. Changing the world up parameter doesn’t change this.

Then you just need to apply an additional rotation to the object after you’ve got your look rotation.

1 Like