Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug Camera not moving in game view

Discussion in 'Scripting' started by JimWebs, Apr 6, 2024.

  1. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    126
    Not sure if this a bug or something I've done that has locked up the camera. Asked this before but still unable to resolve this issue months down the line. I'm trying to move the camera using basic camera.transform.position. It moves in scene view but not in game view. Weird thing is, I can see the x,y digits changing in inspector when game is running in game view but nothing is moving!

    Hoping someone else may have run into similar problem and knows a possible solution or can point me in the right direction.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,031
    I'll take a wild guess and say maybe you have a second camera in scene and that's the one you're watching?

    OR... everything is a child of the camera so moving it does not move what it sees.

    Finally, delete the camera... you better not see anything in that case! If you do, well, start looking for why.

    And just for completeness...

    Camera stuff is pretty tricky... you may wish to consider using Cinemachine from the Unity Package Manager.

    There's even a dedicated forum: https://forum.unity.com/forums/cinemachine.136/

    If you insist on making your own camera controller, the simplest way to do it is to think in terms of two Vector3 points in space: where the camera is LOCATED and where the camera is LOOKING.

    Code (csharp):
    1. private Vector3 WhereMyCameraIsLocated;
    2. private Vector3 WhatMyCameraIsLookingAt;
    3.  
    4. void LateUpdate()
    5. {
    6.   cam.transform.position = WhereMyCameraIsLocated;
    7.   cam.transform.LookAt( WhatMyCameraIsLookingAt);
    8. }
    Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations.