I have a project where the user controls the camera like how you can control the camera in the scene editor in Unity. I am using raycasts to prevent the camera from going through walls, floors and ceilings. The way it works is I send a raycast out at a certain range and if there isn't a hit then the transform will happen normally but won't happen when there is a hit detected. I am using this method for panning left and right and it works perfect for stopping the player from going through walls.I am also using this same method for the mouse wheel zoom and it works perfect as well. The problem occurs when I use this method for stopping the user from going through the ceiling or floor when the user uses vertical translation. The user will sometimes get stuck on the ceiling or floor and I'm not sure why. I have tried a bunch of different things to fix this issue with no luck. If anyone has any idea why this is happening and a possible work around I would appreciate it. Here is my code for vertical translation with the raycasting.
//if the user moves the camera veritcally
if (verticalTranslation.isActivated())
{
float translateY = Input.GetAxis(mouseVerticalAxisName) * verticalTranslation.sensitivity;
RaycastHit hit;
//sends raycast up veritcally
if (translateY > 0)
{
//if raycast hit detected do nothing, else transform position
if (Physics.Raycast(transform.position, Vector3.up, out hit, maxDistance))
{
Debug.LogWarning("stop movement");
Debug.LogWarning(hit.distance);
}
else
{
transform.Translate(0, translateY, 0);
}
}
//sends raycast down veritcally
if (translateY < 0)
{
if (Physics.Raycast(transform.position, -Vector3.up, out hit, maxDistance))
{
Debug.LogWarning("stop movement");
Debug.LogWarning(hit.distance);
}
else
{
transform.Translate(0, translateY, 0);
}
}
}