void ThirdPerson()
{
// Desired distance via mouse wheel
desDist -= Input.GetAxis ("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs (desDist);
desDist = Mathf.Clamp (desDist, minDistance, maxDistance);
finalDist = desDist;
// Horizontal smooth rotation
xAngl = Mathf.LerpAngle (cam.eulerAngles.y, hero.eulerAngles.y, rotationDampening * Time.deltaTime);
// Vertical angle limitation
yAngl = ClampAngle (yAngl, minAngleY, maxAngleY);
// Camera rotation
Quaternion camRot = Quaternion.Euler (yAngl, xAngl, 0);
// Camera height
Vector3 headPos = new Vector3 (0, -heroHeight /1.2f, 0);
// Camera position
Vector3 camPos = hero.position - (camRot * Vector3.forward * desDist + headPos);
// Recalculate hero position
Vector3 trueHeroPos = new Vector3 (hero.position.x, hero.position.y + heroHeight, hero.position.z);
// Check for collision with Linecast
RaycastHit hit;
bool isOk = false;
if ( Physics.Linecast (trueHeroPos, camPos - Vector3.up + Vector3.forward, out hit, collisionLayers.value)) // slightly behind and below the camera
{
// Final distance
finalDist = Vector3.Distance (trueHeroPos, hit.point) - offsetFromWall;
isOk = true;
}
// Lerp current distance if not corrected
if ( !isOk || ( finalDist > curDist ) )
curDist = Mathf.Lerp (curDist, finalDist, Time.deltaTime * zoomDampening);
else
curDist = finalDist;
// Clamp current distance
//curDist = Mathf.Clamp (curDist, minDistance, maxDistance);
// Recalculate camera position
camPos = hero.position - (camRot * Vector3.forward * curDist + headPos);
// Left shift = no y rotation
if ( !Input.GetKey ( KeyCode.LeftShift ) )
{
// Apply Y-mouse axis
yAngl -= Input.GetAxis ( "Mouse Y" ) * ySpeed * 0.02f;
}
// Apply position and rotation
cam.rotation = camRot;
cam.position = camPos;
if (Input.GetAxis("Mouse X") !=0)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * 300 * Time.deltaTime, 0);
}
}