Cannot figure out how to measure the distance of a raycast being sent out to the side of an object

Basically I have a camera in 3d space that the user controls for a 3d house tour. I have a raycast that prevents the user from going through walls that works good using raycast as long as the wall is in front of the user and they are moving forward. My problem is I want to replicate that when the user moves the camera from side to side so the camera will stop when it gets too close to a wall. I figured out how to send the raycast to the left and right of the camera but I cant get the raycast distance using that method. If anyone knows a way to get the distance or a way in general to get the raycast to prevent the user from going through a wall I would really appreciate it.

This is the raycast I want to make work

        if (horizontalTranslation.isActivated())
        {
            float translateX = Input.GetAxis(mouseHorizontalAxisName) * horizontalTranslation.sensitivity;
            transform.Translate(translateX, 0, 0);
            var lft = transform.TransformDirection(Vector3.left);
            var rgt = transform.TransformDirection(Vector3.right);
             //left raycast
            if (Physics.Raycast(transform.position, lft, 4))
            {
                originalRayPos = transform.position;
                originalRayRot = transform.rotation;
                Debug.LogWarning("left raycast set");
            }
            if (Physics.Raycast(transform.position, lft, 1))
            {
                transform.position = originalRayPos;
                transform.rotation = originalRayRot;
                Debug.LogWarning("left raycast fire");
            }
            //right raycast
            if (Physics.Raycast(transform.position, rgt, 4))
            {
                originalRayPos = transform.position;
                originalRayRot = transform.rotation;
                Debug.LogWarning("right raycast set");
            }
            if (Physics.Raycast(transform.position, rgt, 1))
            {
                transform.position = originalRayPos;
                transform.rotation = originalRayRot;
                Debug.LogWarning("right raycast fire");
            }
        }

This is what I did for the raycast for when the camera moves forward and it works perfect I’m just adding it here for reference because this is what I want to accomplish with the other code

            if (scroll.isActivated())
            {
                float translateZ = Input.GetAxis(scrollAxisName) * scroll.sensitivity;
                transform.Translate(0, 0, translateZ);

                Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit))
                {
                    // the object identified by hit.transform was clicked
                    // do whatever you want              
                    if ((hit.distance < 5) && (hit.distance > 1))
                    {
                        originalRayPos = transform.position;
                        originalRayRot = transform.rotation;                               
                    }
                    if(hit.distance < 1)
                    {
                        transform.position = originalRayPos;
                        transform.rotation = originalRayRot;
                    }

                }

            }
        }

Hi @andrewu331, I’m trying to figure out why you can’t get this RayCast hit distance when you cast rays from the left or right side of the camera. hit.distance is the distance you need, just like the RayCast you did using ScreenToPointRay, so where was the problem? What method were you using?

OK, here is a simple solution that does both directions in one shot. I’ve tested this on paper, and it works given the assumptions:

  // assumes player moves right or left parallel to world x
 // get the direction and magnitude of the move
 // use those to determine if player can move
 // uses a padding value to prevent movement too close 
 //    to an obstacle
float translateX = Input.GetAxis(mouseHorizontalAxisName) * horizontalTranslation.sensitivity;

float magnitudeX = Math.Abs( translateX ); // how far?
if( magnitudeX > 0 ) { // if moving
     float direction  = Mathf.Sign( translateX ); // left or right?
     float padding = 1f; // give me some room!
     Vector3 rayCastDir = Vector3.right * direction; // ray direction
     if ( Physics.Raycast( transform.position, rayCastDir, out hit ) ) {
        float distance = hit.distance - padding; // apply padding
        if( distance > 0 ) { // there is room to move
            if( distance < magnitudeX ) { // translateX moves too far
                  // move close as possible
                  translateX = distance * direction; 
            }
        } else translateX = 0; // no room to move
    }
    transform.position = transform.position + new Vector3( translateX, 0, 0 );
}