- Home /
Corner of third person camera clipping through objects
Hello,
I have been working on a third person camera recently. I got the collision working quite easily, but I'm facing some issues with the corner of the camera clipping through objects. I have tried several things to fix it, but so far none have been succesful. Here is an example of what I mean, I've marked the issue with a red circle.
The camera is able to partly see through the wall, while it should be like in this demo: MasterCamera preview (use MouseVerticalAndHorizontal)
Here is my current code. Don't pay too much attention to the comments, I've been editing the code quite often and the comments probably aren't up to date anymore. Also, the code itself needs some cleanup as well. As I said, I've been editing it a lot and it's a bit of a mess right now. I've left out the end, but there is nothing remarkable there. I only set the definitive position there, which in the case of a collision, is based on the code you see here. The camera's near clipping plane is set to 0.1.
//grab x and y values from the mouse
x += Input.GetAxis("Mouse X") * rotSpeed;
y -= Input.GetAxis("Mouse Y") * rotSpeed;
//Make sure the Y value won't exceed 60 degrees. We don't want to be able to spin around the character on the y axis.
y = Mathf.Clamp(y, -60, 60);
//Is the camera looking up at the target?
bool lookFromBelow = Vector3.Angle (transform.forward, target.transform.up * -1) > Vector3.Angle (transform.forward, target.transform.up);
//set camera rotation based on the mouse movement
rotation = Quaternion.Euler(y, x, 0);
/* Here we calculate the position we would like the camera to be in. We do this by doing the following:
We take the rotation we calculated above and multiply it by the direction we want the camera to face, which is always forward.
Then we multiply it once more, but this time with the preferred distance to the target. The final attribute we need is the height
at which the camera will be standing. We need this to be slightly upwards (which means we actually have to put down a negative float) otherwise the target would be blocking a large part of the screen.
After all that we detract the result of this calculation from the targets position. This will give us the position of our camera. */
Vector3 position = target.position - (rotation * Vector3.forward * preferredDistance + new Vector3(0, -1.0f, 0));
//we'll use a raycast to check for any obstructions between the camera and its prey
RaycastHit collisionHit;
//we calculate the targets position taking into account the height of the target
Vector3 targetPosition = new Vector3(target.position.x, target.position.y + 1.0f, target.position.z);
//if there is an obstruction, correct the camera position and calculate the corrected distance
bool isCorrected = false;
//set the corrected distance to the preferred one
float correctedDistance = preferredDistance;
//Just in case we missed something with the sphere cast we'll do a line cast as long as there was no correction by the sphere cast.
if(!isCorrected && Physics.Linecast(target.position, position, out collisionHit, collisionLayer.value))
{
//print ("Linecast collision");
Debug.DrawLine(target.position,collisionHit.point,Color.magenta);
correctedDistance = Vector3.Distance(target.position, collisionHit.point) - wallOffset;
if(Input.GetMouseButton(0))
{
print (this.transform.rotation.x);
}
//print (correctedDistance);
isCorrected = true;
}
/*If the distance hasn't been corrected (no objects between camera and target) or the new corrected distance is larger than the current distance
we use mathf.lerp to interpolate from the current distance back to the distance we actually want. We do this to ensure
that we smoothly go back from a zoomed in state*/
if(!isCorrected || correctedDistance > currentDistance)
{
//We use zoomDampening to allow for customization on the zoom speed
currentDistance = Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomDampening);
}
else
{
//if there has been no correction or the corrected distance is less than the current one, we just set the current value to the corrected one straight away.
currentDistance = correctedDistance;
}
Thanks
Your answer
Follow this Question
Related Questions
Shadow flicker 0 Answers
I cant see my gun its clipping through my camera 0 Answers
Fog Effect Horizon Flicker 0 Answers
How to use two cameras, one for culling one for rendering 2 Answers
Why does setting the Camera Clip Plane make my game run slow? 3 Answers