For the sake of making a third person camera that makes sure there are no “scenery” objects between the player and itself, I have created a CameraControls C# script based on the initial MouseLook script that comes with Unity. Initially, I made a camera that moved logically and stayed focus on the player character:
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
float theta = Mathf.Deg2Rad * rotationX;
float alpha = Mathf.Deg2Rad * rotationY;
float currentLocation = new Vector3( -1 * Mathf.Sin (theta) * Mathf.Cos (alpha), -1 * Mathf.Sin (alpha), -1 * Mathf.Cos (theta) * Mathf.Cos (alpha));
transform.localPosition = currentLocation * cameraDistance;
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
Next, I added a Physics.Raycast to find any collisions between where the camera would be (at maximum distance) and where there player is:
...
currentLocation = new Vector3(...); // same code as before
Ray ray = new Ray(center, currentLocation);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, cameraDistance))
{
transform.localPosition = currentLocation * (ray.origin - hit.point).magnitude;
}
else
{
transform.localPosition = currentLocation * cameraDistance;
}
transform.localEulerAngles = ... // same code as before
...
The problem I’m having is that the camera is working well detecting the cube I have placed and stretched to function as a floor, but doesn’t interact with any of the cubes I have similarly stretched and placed to act as walls. I have made sure that my cubes are all in the same layer, and are not being ignored by the raycast.
The raycast also doesn’t seem to be working well with selective layers either in other tests, but I thought I might solve a more fundamental problem before moving on to that.