Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Objects become invisible depending on camera rotation

Discussion in 'Cinemachine' started by BeorGames, Sep 30, 2023.

  1. BeorGames

    BeorGames

    Joined:
    Jul 28, 2018
    Posts:
    61
    Hey there,

    I'm struhggling with this for quite some time now. I'm using cinemachine to follow the player character and everything works well in 99% of the map, but at the edge of the map somethign really weird happens, depending on the camera rotation some objects become completely invisible, even the player that the camera is tracking can't be seen, here are some screenshots to ilustrate this:
    upload_2023-9-30_12-20-28.png

    Here's the camera on the same rotation, when I select GameObject > Align with view to align the editor camera to the selected object, you can see the camera view on the bottom right of the screen.
    upload_2023-9-30_12-21-40.png

    If I rotate the camera a little more everything goes back to normal:
    upload_2023-9-30_12-22-36.png
    upload_2023-9-30_12-22-56.png

    I have absolutelly no idea how to fix this :p

    Any help is welcome :D
     
  2. qpscnlefogzlrum

    qpscnlefogzlrum

    Joined:
    Oct 1, 2023
    Posts:
    1
    Hello!! Buddy.... Yeah when i was new to unity i was facing a same issue But now i have strong grip on it... so with a passage of time you will learnt these things well cheers some one will help you with all these issue Thank you
     
  3. BeorGames

    BeorGames

    Joined:
    Jul 28, 2018
    Posts:
    61
    Hey buddy, thanks for the motivations speech, but you shouldn't post anything unrelated to the questions asked in the foruns..
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    Can you show an image of the vcam inspector? Also, what version of CM?
     
  5. BeorGames

    BeorGames

    Joined:
    Jul 28, 2018
    Posts:
    61
    Hey, thanks for your reply, sure here it is:
    I'm using the latest version: upload_2023-10-4_11-37-3.png

    here's the Virtual Camera component.
    upload_2023-10-4_11-35-0.png

    Maybe this is also helpful
    upload_2023-10-4_11-35-26.png

    I have a script that does this to the camera on LateUpdate:

    Code (CSharp):
    1.         protected virtual void HandleOffset()
    2.         {
    3.             var target = player.unsizedPosition + Vector3.up * heightOffset;
    4.             var previousPosition = m_cameraTargetPosition;
    5.             var targetHeight = previousPosition.y;
    6.  
    7.             if (player.isGrounded || VerticalFollowingStates())
    8.             {
    9.                 if (target.y > previousPosition.y + verticalUpDeadZone)
    10.                 {
    11.                     var offset = target.y - previousPosition.y - verticalUpDeadZone;
    12.                     targetHeight += Mathf.Min(offset, maxVerticalSpeed * Time.deltaTime);
    13.                 }
    14.                 else if (target.y < previousPosition.y - verticalDownDeadZone)
    15.                 {
    16.                     var offset = target.y - previousPosition.y + verticalDownDeadZone;
    17.                     targetHeight += Mathf.Max(offset, -maxVerticalSpeed * Time.deltaTime);
    18.                 }
    19.             }
    20.             else if (target.y > previousPosition.y + verticalAirUpDeadZone)
    21.             {
    22.                 var offset = target.y - previousPosition.y - verticalAirUpDeadZone;
    23.                 targetHeight += Mathf.Min(offset, maxAirVerticalSpeed * Time.deltaTime);
    24.             }
    25.             else if (target.y < previousPosition.y - verticalAirDownDeadZone)
    26.             {
    27.                 var offset = target.y - previousPosition.y + verticalAirDownDeadZone;
    28.                 targetHeight += Mathf.Max(offset, -maxAirVerticalSpeed * Time.deltaTime);
    29.             }
    30.  
    31.             m_cameraTargetPosition = new Vector3(target.x, targetHeight, target.z);
    32.         }
    33.  
    34.         protected virtual void HandleOrbit()
    35.         {
    36.             if (canOrbit)
    37.             {
    38.                 var direction = player.inputs.GetLookDirection();
    39.  
    40.                 if (direction.sqrMagnitude > 0)
    41.                 {
    42.                     var usingMouse = player.inputs.IsLookingWithMouse();
    43.                     float deltaTimeMultiplier = usingMouse ? Time.timeScale : Time.deltaTime;
    44.  
    45.                     m_cameraTargetYaw += direction.x * deltaTimeMultiplier;
    46.                     m_cameraTargetPitch -= direction.z * deltaTimeMultiplier;
    47.                     m_cameraTargetPitch = ClampAngle(m_cameraTargetPitch, verticalMinRotation, verticalMaxRotation);
    48.                 }
    49.             }
    50.         }
    51.  
    52.         protected virtual void HandleVelocityOrbit()
    53.         {
    54.             if (canOrbitWithVelocity && player.isGrounded)
    55.             {
    56.                 var localVelocity = m_target.InverseTransformVector(player.velocity);
    57.                 m_cameraTargetYaw += localVelocity.x * orbitVelocityMultiplier * Time.deltaTime;
    58.             }
    59.         }
    60.  
    61.         protected virtual void MoveTarget()
    62.         {
    63.             m_target.position = m_cameraTargetPosition;
    64.             m_target.rotation = Quaternion.Euler(m_cameraTargetPitch, m_cameraTargetYaw, 0.0f);
    65.             m_cameraBody.CameraDistance = m_cameraDistance;
    66.         }
    Looking forwartd to hearing from you :D
     
  6. antoinecharton

    antoinecharton

    Unity Technologies

    Joined:
    Jul 22, 2020
    Posts:
    156
    Heyyoo :)

    TWhat's the position of the camera when you have those problems? Also what's the scale of your gameobject?
     
  7. marcoantap

    marcoantap

    Joined:
    Sep 23, 2012
    Posts:
    166
    Sometimes the camera can mark certain game objects as ‘behind it’ and when that happens they will not render on it at all. Does this issue occur if you turn it off by going to the camera in the inspector and unchecking the occlusion culling checkbox?

    A solution: go to the meshrenderer of your game object and check the ‘Update when offscreen’ box, and the object should no longer randomly disappear from the camera. If not available, use mesh.RecalculateBounds(); it lets the camera know where the mesh was.
     
  8. BeorGames

    BeorGames

    Joined:
    Jul 28, 2018
    Posts:
    61
    Hey!

    This happens accross multiple camera positions, nothing extraordinary, they're around -210, 15, 40. The scales range from 1 for the player to 30, 1, 2 for the ground. The camera object scale is 1.

    Yeah! Thanks! Deactivating Occlusion Culling fixes this problem, but 'Update when offscreen' doesn't have any effect.

    I could leave that off, but I'm affraid it might compromise the performance of the game and since this is a going to be a web game, I don't have a lot of memory to dispose. Any other solutions you guys can think of?

    I've checked the whole map and there's absolutely no mesh or collider in front of the camera, but I do have a collider and a rigidbody in the camera, could this cause any issues? (I've tried removing them and the problem persists, but who knows :p)

    Looking forward to hearing from you! :)
     
  9. antoinecharton

    antoinecharton

    Unity Technologies

    Joined:
    Jul 22, 2020
    Posts:
    156
    Ok thought it could be a float rounding issue but those values are pretty standard.

    I could ask graphics what they think. What's your render pipeline?
     
  10. BeorGames

    BeorGames

    Joined:
    Jul 28, 2018
    Posts:
    61
    Thanks! I'm using the standard renderer.
     
  11. antoinecharton

    antoinecharton

    Unity Technologies

    Joined:
    Jul 22, 2020
    Posts:
    156
  12. ValentinHm

    ValentinHm

    Unity Technologies

    Joined:
    Dec 10, 2020
    Posts:
    13
    Hi ! Definitely looks like an Occlusion Culling precision problem, If you would like to use it, I would suggest you to setup Occlusion Areas https://docs.unity3d.com/2023.3/Documentation/Manual/class-OcclusionArea.html and rebake Occlusion, it generates higher precision data and will hopefully fix your problem :)
     
    Gregoryl and antoinecharton like this.
  13. BeorGames

    BeorGames

    Joined:
    Jul 28, 2018
    Posts:
    61
    Hey, thank you so much, I'm a complete noob when it comes to occclusion culing. It turns out that simply baking the Occlusion fixed this issue :D
     
    antoinecharton and ValentinHm like this.