- Home /
Camera Wall Collision Script not Working Properly
This is a coding question. I have here an excerpt from my script that keeps the camera from going through the wall. The variables have been defined, and this is being executed in LateUpdate(). Note that I have a CameraScript (which has the camera tools) and a CameraManagerScript(which tells the CameraScript when to do something). This is from the CameraManagerScript:
RaycastHit[] hits = Physics.RaycastAll (myTrans.position, camTrans.position - myTrans.position, theCamera.distance);
string objectTag;
if (hits.Length > 0){
float minDistance = 100f;
float hitDistance;
int closest = 0;
for( int i = 0; i < hits.Length; ++i){
objectTag = hits[i].collider.gameObject.tag;
if (objectTag == "Terrain"){
hitDistance = Vector3.Distance (myTrans.position, hits[i].point);
if (minDistance > hitDistance){
minDistance = hitDistance;
closest = i;
}
theCamera.CorrectWall (hits[closest].point, minDistance);
print ("IN WALL");
}
}
}
else{
theCamera.CarryOn ();
print ("NOT IN WALL");
}
The script behaves as expected when the character gets close to the wall. However, if the character gets too close to the wall and tries to leave, the camera sticks to the character. Neither of the print statements are executed. This behavior happens almost every time I try to reproduce it, and the nature of the behavior is consistent.
I figure that the problem is most likely with my CameraScript, but if something is wrong with my code I posted above, please let me know.
Your answer
