- Home /
Rotation approximation issues using Quaternion.LookRotation
Hey, I am trying to create a game with custom made gravity. The idea is that the player gravitates towards the closest object that is tagged as an object with gravity. The idea is to set the transform.up of the object to be equal to the normal of the surface that I am gravitating towards. So far so good. The issue comes with setting the transform.up.
If I use:
transform.up = normal
I get an up that works, but every 180 degrees it will do a full barrel roll, because it doesn't know where to place the forward vector:
Quaternion rotateZToUp = Quaternion.LookRotation(player.GetComponent<Gravity>().averageNormal, -player.transform.forward);
Quaternion rotateYToZ = Quaternion.Euler(90f, 0f, 0f);
transform.rotation = rotateZToUp * rotateYToZ;
I am now approximating a forward which when walking on flat ground makes the camera occasionally spin uncontrollably.
If you're wondering why I am not lerping to this new rotation, the normal that makes up my new up is an average created from a Queue of normals that i gather over time. This ensures a smooth transition if I for example walk over the edge of a cube and "fall" down onto the other surface. This averaging makes it so that if the cube is the correct size my capsule will enter orbit around it. All I want is for the camera to not shift right and left but still follow the up of the capsule as in this picture:
This works as intended when using the Quaternion calculation above, but i get this extreme spinning when walking on the surface of the cube.