Following the video, my UI is pointing in a different direction that facing the camera.
so far, I’ve:
Checked which transform the script is using to look at (it’s definitely camera main, used SerializeField to confirm)
Copied and Pasted the LookAt script, no change
Turns out, the main camera’s position wasn’t good for me. I had to pass the cinemachine virtual camera’s transform to resolve. I had to turn the CameraController into a singleton and get the cinemachine transform from there on Start().
That’s strange, isn’t your Camera positioned exactly on the same position as the Cinemachine Virtual Camera? But mainCamera.transform.position is different from cinemachineVirtualCamera.transform.position?
Perhaps you added some offset somewhere although no idea where, the camera should really be exactly where the virtual camera is.
The main camera and the Cinemachine Virtual Camera both have the same transform values. I’ve tested it again by changing from referencing the cinemachine’s transform (which works well) to the Camera.main.transform and the bug returns. No changes to the calculations, just which transform it references (which in the inspector have the same values).
Can you post a screenshot?
What if instead of transform.LookAt(targetTransform); you do transform.LookAt(targetTransform.position); ?
If the position is correct then it has to work. Perhaps you added an offset in the UI element itself? Maybe you accidentally positioned the UI elements off to the side instead of near the center pivot?
Also make sure you’re actually looking at the pivot of the selected object, not the visual center Code Monkey - Unity Tip: Don't go insane! Learn about these buttons!
What if you rotate/move the camera, what happens? Do they follow the camera? Or stay fixed?
What if you disable the invert?
Maybe you have a completely separate object with the MainCamera tag, add a Debug.Log(cameraTransform); on the Awake, what does it say?
Add a Debug.Log(cameraTransform.position); pause the game and look at the camera, is it in the correct position?
The only other possible explanation I can think of is you have some other script also rotating that object, so that script runs, rotates correctly, then another script runs and messes up the rotation.
Maybe try adding that script to the Script Execution Order and make it run after everything.
Can you make a video of you moving the camera around? I don’t see how it’s possible that it tries to follow when moving and rotating but doesn’t move when zooming.
I don’t know what object you’re referencing but it isn’t the main camera. Look in the inspector of that video, you have the main camera selected, look at the transform position and see how it’s different from the values you’re using in the script.
Note how when you zoom in/out the Camera transform changes correctly but the object you’re following does not change.
Note how the rotation is completely different.
You must have some other object also using the MainCamera tag. Did you do Debug.Log(cameraTransform); and it said “Main Camera (UnityEngine.Transform)”? If so then search in the hierarchy for another object named “Main Camera”, you should find 2
Or instead of getting the object through the tag grab the reference directly. Add a [SerializeField] private Transform cameraTransform; Then in the editor drag that Main camera object you have selected and remove the cameraTransform = Camera.main.transform;
Or instead of getting the object through the tag grab the reference directly. Add a [SerializeField] private Transform cameraTransform; Then in the editor drag that Main camera object you have selected and remove the cameraTransform = Camera.main.transform;
I can’t figure how to attach it to a prefab. I added it to CameraController and made CameraController a singleton. Had each LookAt use that camera, still no improvement.
You must have some other object also using the MainCamera tag. Did you do Debug.Log(cameraTransform); and it said “Main Camera (UnityEngine.Transform)”? If so then search in the hierarchy for another object named “Main Camera”, you should find 2
That is extremely strange, the only possible scenario I can think of is some other script moving the Main Camera then the LookAt script runs, looks at that position, and finally at the end the Cinemachine brain runs and moves the camera again.
Try adding that LookAt script to the Script Execution Order and make it run after everything.
Also what if you try looking at a completely different object? Create an empty game object, make that the target, and move that object around, do the objects with LookAt correctly look at that object?