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.

93 lines
2.6 KiB

7 years ago
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. }
  46. float currentHeight = transform.position.y;
  47. float currentDistance = transform.position.z;
  48. float currentSide = transform.position.x;
  49. // Damp the height
  50. currentHeight = Mathf.Lerp(currentHeight, wantedPos.y, damping.y * Time.deltaTime);
  51. currentDistance = Mathf.Lerp(currentDistance, wantedPos.z, damping.z * Time.deltaTime);
  52. currentSide = Mathf.Lerp(currentSide, wantedPos.x, damping.x * Time.deltaTime);
  53. // Set the position of the camera on the x-z plane to:
  54. // distance meters behind the target
  55. transform.position = target.position;
  56. // Set the height of the camera
  57. transform.position = new Vector3(currentSide, currentHeight, currentDistance);
  58. // Always look at the target
  59. Vector3 lookTarget = target.position;
  60. lookTarget.z = transform.position.z;
  61. transform.LookAt(lookTarget);
  62. }
  63. private Vector3 RotatePointAroundPivot(Vector3 point,Vector3 pivot,float angles ) {
  64. Vector3 dir = point - pivot; // get point direction relative to pivot
  65. dir = Quaternion.Euler(angles * Vector3.up) * dir; // rotate it
  66. point = dir + pivot; // calculate rotated point
  67. return point; // return it
  68. }
  69. }