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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Camera allows scrolling backwards when camera's raycast hits something behind?

Discussion in 'Scripting' started by AerionXI, Apr 11, 2022.

  1. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    when a ray hits and i zoom out and when there's an object behind the camera, how can i prevent the camera from zooming out but at the same time, allow for the camera to zoom in to the target?
    how would i stop allowing the player to zoom out while the raycast is hitting an object, but allow the player to be zoomed in?
    As you can see in the video clip, I can still zoom in and out, but if I zoom out too far, then I try to zoom back in, it waits until `desiredDistance` reaches a certain zoom distance and THEN allows me to zoom the camera back into the target.

    so when i zoom in like this, how can i stop the zoom number from going any further back until it is zoomed in forward again?

    Code (Csharp):
    1.  
    2. public float zoomRate = 10.0f;
    3. public float zoomMindistance = 0.1f;
    4. public float zoomMaxdistance = 34.0f;
    5.  
    6. [ HideInInspector ]
    7. public float desiredDistance = 0.0f;
    8.  
    9. [ HideInInspector ]
    10. public float correctedDistance = 0.0f;
    11.  
    12. [ HideInInspector ]
    13. public float zoomDistance = 0.0f;
    14.  
    15. public float GetPercentage ( float value, float percentageMultiplier = 100.0f, float percentageDivider = 100.0f ) {
    16.     return ( Mathf.Round ( value * percentageMultiplier ) / percentageDivider );
    17. }
    18.  
    19. private float CalculateZoom ( ) {
    20.     // Calculate the desired distance
    21.     desiredDistance = GetPercentage ( desiredDistance );
    22.     Debug.Log ( $"desiredDistance : { desiredDistance }" );
    23.     desiredDistance -= GetAxis ( "Mouse ScrollWheel" ) * zoomRate;
    24.     desiredDistance = Mathf.Clamp ( desiredDistance, zoomMindistance, zoomMaxdistance );
    25.     correctedDistance = desiredDistance;
    26.     return correctedDistance;
    27. }
    28.  
    29. void Start ( ) {
    30.     desiredDistance = zoomDistance;
    31.     correctedDistance = zoomDistance;
    32. }
    33.  
    34. void LateUpdate ( ) {
    35.     zoomDistance = CalculateZoom ( );
    36. }
    37.  
    Here's a small video showing what the camera is doing when zooming out while the camera's raycast hits an object behind the camera.



    I would like to fix MY ABOVE CODE! NOT ANYthing else!

    Thanks!
     
  2. digiholic

    digiholic

    Joined:
    Nov 23, 2013
    Posts:
    14
    It seems like you're just trying to implement something Cinemachine does right out of the box. I would recommend just using Cinemachine instead.
     
  3. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    I don't want to use Cinemachine. I simply want to fix my above code.
     
  4. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    If someone could actually help me with this, I would greatly appreciate it!
     
  5. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    It's been 24 hours. Small bump.