SphereCast issue on collision detection between camera and object

Hi!

I have to detect if any obstacle is present between the camera and the object it is following. I found a little script to achieve the detection and with the linecast it works perfectly, to gain more precision I want to use a spherecast but this doesn't work in the right way... what can I do? Here is the code attached to my camera, the camera is in the Object's hierarcy as its descendant.

var target : Transform;

// the height we want the camera to be above the target
var height = 5.0;
var heightDamping = 2.0;
var rotationDamping = 3.0;

//camera collision variables
var maxDistance = 5.0;
var minDistance = 1.2;
var smooth = 8.0;

private var dollyDir;
private var distance;

// Place the script in the Camera-Control group in the component menu
@script AddComponentMenu("Camera-Control/Smooth Follow")

function Awake() 
{
    dollyDir = transform.localPosition.normalized;
    distance = transform.localPosition.magnitude;
}

function Update () 
{
// Early out if we don't have a target
if (!target)
    return;

//collision policy

//ideal camera position
var desiredCameraPos:Vector3;
var targetDirection:Vector3;
desiredCameraPos= transform.parent.TransformPoint( dollyDir * maxDistance );
targetDirection = target.transform.position - transform.position;
targetDirection = transform.InverseTransformDirection(targetDirection.normalized);

var hit:RaycastHit;
var collflag = Physics.SphereCast( desiredCameraPos, 0.5, targetDirection, hit, maxDistance );
//Physics.Linecast( transform.parent.position, desiredCameraPos,  hit );

Debug.Log("camera pos: "+transform.position +"target pos:"+target.transform.position);
if( collflag )
{       
    var d;
    d = Mathf.Clamp( hit.distance, minDistance, maxDistance );  
    distance = Mathf.Lerp(distance,d,Time.deltaTime*smooth);        
    Debug.Log("camera collision:"+hit.collider.name + " distance:" +distance);
}
else
{
    distance=Mathf.Lerp(distance,maxDistance,Time.deltaTime*smooth);
}

//camera movement policy

// Calculate the current rotation angles
wantedRotationAngle = target.eulerAngles.y;
wantedHeight = target.position.y + height;

currentRotationAngle = transform.eulerAngles.y;
currentHeight = transform.position.y;

// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

// Damp the height
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);

// Convert the angle into a rotation
currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);

// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;

// Set the height of the camera
transform.position.y = currentHeight;

thanks for helping!

D.

Hi!

I haven’t tested it but it looks like you are spherecasting from the desired position instead of the current position:

var collflag = Physics.SphereCast( desiredCameraPos, 0.5, targetDirection, hit, maxDistance );

should be:

var collflag = Physics.SphereCast( transform.parent.position, 0.5, targetDirection, hit, maxDistance );