|
@ -12,6 +12,8 @@ public class CustomSmoothFollow : MonoBehaviour |
|
|
public Vector3 damping; |
|
|
public Vector3 damping; |
|
|
public string layerToMask = "CameraObstruct"; |
|
|
public string layerToMask = "CameraObstruct"; |
|
|
private int layerMask; |
|
|
private int layerMask; |
|
|
|
|
|
[Range (0,360)] |
|
|
|
|
|
public float offsetAngle = 90; |
|
|
|
|
|
|
|
|
// Use this for initialization
|
|
|
// Use this for initialization
|
|
|
void Start() |
|
|
void Start() |
|
@ -33,6 +35,10 @@ public class CustomSmoothFollow : MonoBehaviour |
|
|
|
|
|
|
|
|
bool isBlocked = false; |
|
|
bool isBlocked = false; |
|
|
Vector3 wantedPos = new Vector3(wantedSide, wantedHeight, wantedDistance); |
|
|
Vector3 wantedPos = new Vector3(wantedSide, wantedHeight, wantedDistance); |
|
|
|
|
|
|
|
|
|
|
|
wantedPos = RotatePointAroundPivot(wantedPos, target.transform.position, offsetAngle); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RaycastHit hitInfo; |
|
|
RaycastHit hitInfo; |
|
|
if (Physics.Raycast(wantedPos, target.position-wantedPos, out hitInfo, (target.position-wantedPos).magnitude * 2f)) |
|
|
if (Physics.Raycast(wantedPos, target.position-wantedPos, out hitInfo, (target.position-wantedPos).magnitude * 2f)) |
|
|
{ |
|
|
{ |
|
@ -50,21 +56,37 @@ public class CustomSmoothFollow : MonoBehaviour |
|
|
float currentDistance = transform.position.z; |
|
|
float currentDistance = transform.position.z; |
|
|
float currentSide = transform.position.x; |
|
|
float currentSide = transform.position.x; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Damp the height
|
|
|
// Damp the height
|
|
|
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, damping.y * Time.deltaTime); |
|
|
|
|
|
currentDistance = Mathf.Lerp(currentDistance, wantedDistance, damping.z * Time.deltaTime); |
|
|
|
|
|
currentSide = Mathf.Lerp(currentSide, wantedSide, damping.x * Time.deltaTime); |
|
|
|
|
|
|
|
|
currentHeight = Mathf.Lerp(currentHeight, wantedPos.y, damping.y * Time.deltaTime); |
|
|
|
|
|
currentDistance = Mathf.Lerp(currentDistance, wantedPos.z, damping.z * Time.deltaTime); |
|
|
|
|
|
currentSide = Mathf.Lerp(currentSide, wantedPos.x, damping.x * Time.deltaTime); |
|
|
|
|
|
|
|
|
// Set the position of the camera on the x-z plane to:
|
|
|
// Set the position of the camera on the x-z plane to:
|
|
|
// distance meters behind the target
|
|
|
// distance meters behind the target
|
|
|
transform.position = target.position; |
|
|
transform.position = target.position; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set the height of the camera
|
|
|
// Set the height of the camera
|
|
|
transform.position = new Vector3(currentSide, currentHeight, currentDistance); |
|
|
transform.position = new Vector3(currentSide, currentHeight, currentDistance); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Always look at the target
|
|
|
// Always look at the target
|
|
|
Vector3 lookTarget = target.position; |
|
|
Vector3 lookTarget = target.position; |
|
|
lookTarget.x = transform.position.x; |
|
|
|
|
|
|
|
|
lookTarget.z = transform.position.z; |
|
|
transform.LookAt(lookTarget); |
|
|
transform.LookAt(lookTarget); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Vector3 RotatePointAroundPivot(Vector3 point,Vector3 pivot,float angles ) { |
|
|
|
|
|
Vector3 dir = point - pivot; // get point direction relative to pivot
|
|
|
|
|
|
dir = Quaternion.Euler(angles * Vector3.up) * dir; // rotate it
|
|
|
|
|
|
point = dir + pivot; // calculate rotated point
|
|
|
|
|
|
return point; // return it
|
|
|
|
|
|
} |
|
|
} |
|
|
} |