FPS camera collision

I would like a third person controller with the camera following slightly behind, but if my player backs into a wall, the camera’s follow distance would shorten accordingly.

How is the best way to implement this? Should I put another CharacterController on the camera? Could I simply use the Raycast method of the Physics class?

It seems that this would be a common issue. Is there a thread that discusses this particular camera behavior?

Thanks for any suggestions.

I think raycasting in all directions around your camera, and changing it’s position acordingly is the best way to go. I will have to do a similar thing soon.

Hope that helps.

Bill

I think that the best is use a raycast from the player to the camera and put the camera in the:
Max distance or Collision distance

I’m now implementing one of this ^^.

I use a phyisics based camera for my racing game. The camera has a rigidbody and collider on it so it never goes inside objects. Here is the script:

var target : Transform;
var body : GameObject;

var distance = 0.000;
var normalTargetDistance = 7.00;
var targetDistance = 7.00;

var forceFactor = 0.00;

var inwardForce = 160.00;
var upwardForce = 40.00;
var lookDamping = 4.80;
var normalDrag = 6;

var airMode = false;

rigidbody.freezeRotation = true;

function FixedUpdate()
{
	script = body.GetComponent(Car);
	if(script) airMode = script.airMode;

	if(airMode)
	{
		targetDistance += Time.deltaTime * 4;
		rigidbody.drag += Time.deltaTime * 2;
	}
	else
	{
		targetDistance = normalTargetDistance;
		rigidbody.drag = normalDrag;
	}
	rotation = Quaternion.LookRotation(target.position - transform.position);
	transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * lookDamping);
	transform.localEulerAngles.z = 0;

	distance = Vector3.Distance(transform.position, target.transform.position);


	if(targetDistance < distance) forceFactor = distance - targetDistance;
	if(targetDistance > distance) forceFactor = 0;

	rigidbody.AddRelativeForce (Vector3.forward * inwardForce * forceFactor * Time.deltaTime);
	if(transform.localEulerAngles.x < 30) rigidbody.AddRelativeForce (Vector3.up * upwardForce * Time.deltaTime);
}