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;
}
}
}
}