You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.7 KiB

7 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CustomSmoothFollow : MonoBehaviour
  5. {
  6. public Transform target;
  7. public float distance = -200.0f;
  8. public float height = 200.0f;
  9. public float altDistance = -100.0f;
  10. public float altHeight = 250.0f;
  11. public Vector3 damping;
  12. public string layerToMask = "CameraObstruct";
  13. private int layerMask;
  14. [Range (0,360)]
  15. public float offsetAngle = 90;
  16. // Use this for initialization
  17. void Start()
  18. {
  19. layerMask = LayerMask.NameToLayer(layerToMask);
  20. }
  21. // Update is called once per frame
  22. void LateUpdate()
  23. {
  24. // Early out if we don't have a target
  25. if (!target)
  26. return;
  27. // Calculate the current and wanted height / XZ pos
  28. float wantedHeight = target.position.y + height;
  29. float wantedDistance = target.position.z + distance;
  30. float wantedSide = target.position.x;
  31. bool isBlocked = false;
  32. Vector3 wantedPos = new Vector3(wantedSide, wantedHeight, wantedDistance);
  33. wantedPos = RotatePointAroundPivot(wantedPos, target.transform.position, offsetAngle);
  34. RaycastHit hitInfo;
  35. if (Physics.Raycast(wantedPos, target.position-wantedPos, out hitInfo, (target.position-wantedPos).magnitude * 2f))
  36. {
  37. // Debug.LogWarning (hitInfo.collider.name + " -- " + hitInfo.collider.gameObject.layer + " -- " + layerMask);
  38. if (hitInfo.collider.gameObject.layer == layerMask)
  39. isBlocked = true;
  40. }
  41. if (isBlocked)
  42. {
  43. wantedPos.y = target.position.y + altHeight;
  44. wantedPos.z = target.position.z + altDistance;
  45. wantedPos.x = target.position.x;
  46. wantedPos = RotatePointAroundPivot(wantedPos, target.transform.position, offsetAngle);
  47. }
  48. float currentHeight = transform.position.y;
  49. float currentDistance = transform.position.z;
  50. float currentSide = transform.position.x;
  51. // Damp the height
  52. currentHeight = Mathf.Lerp(currentHeight, wantedPos.y, damping.y * Time.deltaTime);
  53. currentDistance = Mathf.Lerp(currentDistance, wantedPos.z, damping.z * Time.deltaTime);
  54. currentSide = Mathf.Lerp(currentSide, wantedPos.x, damping.x * Time.deltaTime);
  55. // Set the position of the camera on the x-z plane to:
  56. // distance meters behind the target
  57. transform.position = target.position;
  58. // Set the height of the camera
  59. transform.position = new Vector3(currentSide, currentHeight, currentDistance);
  60. // Always look at the target
  61. Vector3 lookTarget = target.position;
  62. lookTarget.z = transform.position.z;
  63. transform.LookAt(lookTarget);
  64. }
  65. private Vector3 RotatePointAroundPivot(Vector3 point,Vector3 pivot,float angles ) {
  66. Vector3 dir = point - pivot; // get point direction relative to pivot
  67. dir = Quaternion.Euler(angles * Vector3.up) * dir; // rotate it
  68. point = dir + pivot; // calculate rotated point
  69. return point; // return it
  70. }
  71. }