I'm using the following camera code from Unitylabs: http://www.unitylabs.net/tutorials/gameplay-mechanics/third-person-camera-controller
But the tutorial was never finished. Could someone tell me how to make it to where it won't collide with the walls and will rotate to a more suitable direction.
@script AddComponentMenu("Camera-Control/Camera Controller")
var target : Transform;
private var distance = 2.0;
private var xSpeed = 250.0;
private var ySpeed = 120.0;
private var yMinLimit = 0;
private var yMaxLimit = 80;
private var cameraMode = "third";
private var zoomMinLimit = 2;
private var zoomMaxLimit = 6;
private var x = 0.0;
private var y = 0.0;
function Start ()
{
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
function LateUpdate ()
{
if(cameraMode == "third")
{
var characterRotation = target.transform.eulerAngles.y;
var cameraRotation = transform.eulerAngles.y;
if(Input.GetButton("CameraZoomIn") && distance > zoomMinLimit)
{
distance = distance -1 / (xSpeed * 0.02);
}
if(Input.GetButton("CameraZoomOut") && distance < zoomMaxLimit)
{
distance = distance +1 / (xSpeed * 0.02);
}
x += Input.GetAxis("CameraX") * xSpeed * 0.02;
y -= Input.GetAxis("CameraY") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal"))
{
rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(y, characterRotation, 0), Time.deltaTime * 3);
position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
x = characterRotation;
}
else
{
rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(y, x, 0), Time.deltaTime * 3);
position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
}
transform.rotation = rotation;
transform.position = position;
}
else
{
}
}
static function ClampAngle (angle : float, min : float, max : float)
{
if (angle < -360)
{
angle += 360;
}
if (angle > 360)
{
angle -= 360;
}
return Mathf.Clamp (angle, min, max);
}