So I have this mesh that works as a watersurface. There are waves propagating through the level, so the surface changes constantly.
And I have a boat that I want to align to the water surface.
Therefor the boat checks the height of the water at four positions (front, back, left right). I wanted to set the forward vector and the right vector of the boats transform to be the Delta between front ↔ back and left ↔ right respectively. But this does not work as Setting the forward vector will zero out / reset the right vector and vice versa. So theres always only one direction correct, the other flat.
Any ideas how to assign both of them at the same time?
Thanks
Haven’t tested it but something like this:
Vector3 rightProjected = Vector3.ProjectOnPlane(desiredForwardVector, desiredRightVector).normalized; // makes sure the vectors are perpendicular. You can skip this if you are already sure they are.
transform.rotation = Quaternion.LookRotation(desiredForwardVector, rightProjected); // sets the forward vector to desiredForwardVector and the up vector to rightProjected. Now we need to rotate it so the right vector is at rightProjected
transform.rotation *= Quaternion.AngleAxis(-90f/*this might need to be positive*/, transform.forward);
3 Likes
You can use Vector3.Cross(forward, right) to get the up (or normal) of your boat, then feed that into the second parameter of Quaternion.LookRotation(forward).
2 Likes