LookAt Script Pointing A Different Direction

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().

Any idea why I had to do this?

1 Like

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.

1 Like

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).

I’m honestly stumped at what is going on.

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!

1 Like

When i switched to LookAt(cameraTransform.position)

Code

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?

  • UI moves if I move camera:
    • will attempt to follow when camera is moved along x, z
    • ignores camera zoom movement
    • moves when camera rotated
  • disabling invert doesn’t resolve. It just doesn’t invert
  • I considered another object also had the main camera tag, but I couldn’t find one
  • console says: Main Camera (UnityEngine.Transform)

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.

1 Like

https://drive.google.com/file/d/1qXrhV3KcsrNATo8WkiF4i0TKdIKZPhmq/view?usp=sharing

Please let me know if that doesn’t work

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;

1 Like

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

I added a debug log to Testing to list all game objects with MainCamera:

    private void Start()
    {
        var items = GameObject.FindGameObjectsWithTag("MainCamera");
        Debug.LogWarning("Found items: " + items.Length.ToString());
    }

image

I’m thinking something else affecting must be the key, or something so simple I’m completely missing it.

Here’s the camera controller:

public class CameraController : MonoBehaviour
{
    public static CameraController Instance;

    private const float MIN_FOLLOW_Y_OFFSET = 2.0f;
    private const float MAX_FOLLOW_Y_OFFSET = 24.0f;

    public Camera mainCamera;
    [SerializeField] private CinemachineVirtualCamera cinemachineVirtualCamera;
    private CinemachineTransposer transposer;

    private Vector3 targetFollowOffset;

    [SerializeField] private float moveSpeed = 8.0f;
    [SerializeField] private float rotationSpeed = 100.0f;
    [SerializeField] private float zoomSpeed = 5.0f;

    private void Awake() {
        if (Instance == null) Instance = this;
    }

    private void Start() { 
        transposer = cinemachineVirtualCamera.GetCinemachineComponent<CinemachineTransposer>();
        targetFollowOffset = transposer.m_FollowOffset;
    }

    // Update is called once per frame
    void Update() {
        HandleMovement();
        HandleRotation();
        HandleZoom();
    }

    private void HandleMovement() {
        Vector3 inputMoveDirection = Vector3.zero;

        if (Input.GetKey(KeyCode.W)) {
            inputMoveDirection.z = +1.0f;
        }

        if (Input.GetKey(KeyCode.S)) {
            inputMoveDirection.z = -1.0f;
        }
        if (Input.GetKey(KeyCode.A)) {
            inputMoveDirection.x = -1.0f;
        }
        if (Input.GetKey(KeyCode.D)) {
            inputMoveDirection.x = +1.0f;
        }

        inputMoveDirection.Normalize();

        Vector3 moveVector = transform.forward * inputMoveDirection.z + transform.right * inputMoveDirection.x;
        transform.position += moveVector * moveSpeed * Time.deltaTime;
    }

    private void HandleRotation() {

        Vector3 rotationVector = Vector3.zero;

        if (Input.GetKey(KeyCode.Q)) {
            rotationVector.y = +1.0f;
        }
        if (Input.GetKey(KeyCode.E)) {
            rotationVector.y = -1.0f;
        }

        transform.eulerAngles += rotationVector * rotationSpeed * Time.deltaTime;
    }

    private void HandleZoom() {


        targetFollowOffset.y = targetFollowOffset.y - Input.mouseScrollDelta.y;

        targetFollowOffset.y = Mathf.Clamp(targetFollowOffset.y, MIN_FOLLOW_Y_OFFSET, MAX_FOLLOW_Y_OFFSET);
        transposer.m_FollowOffset = Vector3.Lerp(transposer.m_FollowOffset, targetFollowOffset, zoomSpeed * Time.deltaTime);
    }
}

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?

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms