Hello Unity Answers!
I have a character who moves on the surface of a globally fixed cube. I want the camera to look straight on the character and follow the characters movements by circling around origo and look at the character. I don’t want it to rotate the camera if the character rotates etc but just following the movements of the character on the cube.
I have implemented this using transform.LookAt(origo) but my problem is that the camera flips when the character has moved so that the camera would have its upward vector downwards in the global coordinates (because of how LookAt is implemented).
Well, it’s pretty obvious Do you see your little arrow that points up? That’s what you pass as “world_up”. Since that vector most likely is just Vector3.up (so in worldspace it’s simply up) you get exactly what you described in your lower images.
The up vector just controls the rotation around the look vector. So you define that you want to look at “this” point and the up vector tell the function “in which direction should be up”. If you want the behaviour you’ve described in your upper images you have to pass the “correct” up vector. However from the information provided you can’t determine which is “correct”.
It looks a bit like you want to have an orbit camera around that point. In this case it’s way easier to place an empty gameobject at the point you want to look at, make the camera a child of it, place it locally at (0, 0, -distanceFromOjbect) by setting the localPosition of the camera to this vector and finally just rotate the empty gameobject manually.
If you use LookAt you have to provide an up vectot that represents the up you want. If you can’t do that, don’t use lookat
I did eventually solve this a few years ago
My character is walking around a cube and my method was to store the difference vector between the two faces that the character was traveling between and use this as the up vector.
The big advantage of this is that the camera doesn’t move so much and it looks very natural when you move around a lot on one face and then move to the next face. The camera stays put nicely on one face and can quickly move to the next without causing confusion and it was quite easy to achieve.
If you have a sphere or any other shape you can do this as well by introducing a cube. I feel there must be a better solution though but I’ll just leave this here anyways.