Third person camera collision problem
Hello, I have decided to code my own script to check whether or not my character is being fully seen by my camera. It's a third person camera which follows and orbits around the player. I've successfully managed to do so to a certain extent. My problem is that the rays which go from the player to the camera to check if nothing is in the way do not work properly. In fact, they sometimes randomly stop detecting a collision at some random frame.
The code looks like this :
// Update is called once per frame
void LateUpdate () {
//Check if a collision happened
bool checkIfCollision = false;
//Clip points
ClipPlanePoints nearPlanePointsCheck = cameraClipPlanePoints (0.35f);
//Debug.Log (nearPlanePointsCheck.LowerLeft + ", " + nearPlanePointsCheck.LowerRight + ", " + nearPlanePointsCheck.UpperLeft + ", " + nearPlanePointsCheck.UpperRight);
Debug.DrawLine (target.position, nearPlanePointsCheck.LowerLeft);
Debug.DrawLine (target.position, nearPlanePointsCheck.LowerRight);
Debug.DrawLine (target.position, nearPlanePointsCheck.UpperLeft);
Debug.DrawLine (target.position, nearPlanePointsCheck.UpperRight);
//Check where the ray hit
RaycastHit hit;
//List of points
List<float> hitDistances = new List<float>();
//Check if lower left hit
if (Physics.Raycast (target.position, (nearPlanePointsCheck.LowerLeft - target.position), out hit, distanceFromTarget, clippable.value)) {
Debug.DrawLine (target.position, hit.point, Color.black);
checkIfCollision = true;
hitDistances.Add (hit.distance);
}
//Check if lower right hit
if (Physics.Raycast (target.position, (nearPlanePointsCheck.LowerRight - target.position), out hit, distanceFromTarget, clippable.value)) {
Debug.DrawLine (target.position, hit.point, Color.black);
checkIfCollision = true;
hitDistances.Add (hit.distance);
}
//Check if upper left hit
if (Physics.Raycast (target.position, (nearPlanePointsCheck.UpperLeft - target.position), out hit, distanceFromTarget, clippable.value)) {
Debug.DrawLine (target.position, hit.point, Color.black);
checkIfCollision = true;
hitDistances.Add (hit.distance);
}
//Check if upper right hit
if (Physics.Raycast (target.position, (nearPlanePointsCheck.UpperRight - target.position), out hit, distanceFromTarget, clippable.value)) {
Debug.DrawLine (target.position, hit.point, Color.black);
checkIfCollision = true;
hitDistances.Add (hit.distance);
}
//Sort points to get the shortest one
if (hitDistances.Count != 0 && hitDistances != null) {
for (int x = 0; x < hitDistances.Count; x++) {
if (hitDistances [0] >= hitDistances [x] && (hitDistances [0] - hitDistances[x]) > 0.005f ) {
hitDistances[0] = hitDistances[x];
Debug.Log(hitDistances[0]);
}
}
}
Debug.Log (checkIfCollision);
//Check if camera collids with Clippable layer
if (checkIfCollision == true) {
//Get the distance between player and hit point
float dis = hitDistances[0];
//Debug.LogWarning (dis);
float newPosition = Mathf.SmoothDamp(distanceFromTarget, dis, ref yVelocity, smoothTimeIn);
//New distance to place camera in
distanceFromTarget = newPosition;
} else if (checkIfCollision == false) {
//Dezoom from collision
float normalPosition = Mathf.SmoothDamp(distanceFromTarget, idealDistanceFromTarget, ref yVelocity, smoothTimeOut);
distanceFromTarget = normalPosition;
}
//Movement of the camera which influences the player if he's moving, x or y
horizontalMouse += Input.GetAxis ("Mouse X");
verticalMouse += Input.GetAxis ("Mouse Y");
//Clamp y
verticalMouse = ClampAngle(verticalMouse, yMinLimit, yMaxLimit);
//Calculating the rotation of the camera with smooth transition
currenRotation = Vector3.SmoothDamp(currenRotation, new Vector3 (verticalMouse, horizontalMouse), ref rotationSmoothVelocity,rotationSmoothTime);
//Rotate the camera
transform.eulerAngles = currenRotation;
//Position of the camera
transform.position = target.position - transform.forward * distanceFromTarget;
}
The camera keeps going from the collision position to the normal one at some frames and I don't know why. Thanks for your help.
Your answer
![](https://koobas.hobune.stream/wayback/20220612124747im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using a second camera and finding it using a tag 0 Answers
How can I make a third person camera collision script? 2 Answers
Why object's doesn't stop on collision enter? 0 Answers
uNet hitboxes and movement. 0 Answers
How to do a wall jump? i am very novice 0 Answers