Ray Cast not working for camera.


I am trying to make a 3rd person camera script. But it does not work and I don’t know what I am doing wrong.

First the explanation: There is a far-cam-point, this is the max distance between player and camera. The close cam point is the same with z=0. If the camera hits a wall, I just need to apply the (negative) distance between the closecampoint and the hit of the raycast to z of farcampoint and can then set the cam position to this new farcampoint.

I draw Debug-Lines all over here: Cyan is farcampoint to closecampoint. Yellow is closecampoint to hit and red is the raycast itself. All lines are aligned over each other, so that is fine. But it doesnt seem to hit anything when I approach a wall. Any help with this would be great.

(Ah, yes…lerp and slerp will come later… :wink: )

Here is the code:
Vector3 farCamPoint = player.position + camYRotation * pivotOffset+ aimRotation * camOffset;
Vector3 closeCamOffset=new Vector3(camOffset.x, camOffset.y, 0.0f);
Vector3 closeCamPoint= player.position+camYRotationpivotOffset+aimRotationcloseCamOffset;

	Debug.DrawLine(farCamPoint, closeCamPoint, Color.cyan);
	RaycastHit hit;
	Vector3 raycastDir = farCamPoint - closeCamPoint;
	float distance=Vector3.Distance (farCamPoint, closeCamPoint);
	float padding = 0.3f;
	if (Physics.Raycast(closeCamPoint, raycastDir, out hit, distance, mask)) {
		farCamPoint=new Vector3(camOffset.x, camOffset.y, - Vector3.Distance (closeCamPoint, hit.point));
		Debug.Log ("Hit the wall");
	Debug.DrawLine(farCamPoint, closeCamPoint, Color.yellow);

	Debug.DrawRay (closeCamPoint, raycastDir,Color.red);


It was the mask as you mentioned. I just copied it from the camera script in the 3rdPersonShooter example where it is also used for aiming at stuff. (Thats done in another script by me…)

so, set the mask to everything and it works.


and it works… (not really tested yet but it already moves the camera…anywhere…hope its the right spot…i got another ray just now, hope it works with the ray above. :wink: )