| | 1 | | using System; |
| | 2 | | using Cinemachine.Utility; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | public class FollowWithDamping : MonoBehaviour |
| | 6 | | { |
| | 7 | | public Transform target; |
| | 8 | | public Vector3 damping; |
| 598 | 9 | | public float dampingChangeSpeed = 0.5f; |
| | 10 | |
|
| | 11 | | [Header("Debug Zone")] |
| | 12 | | public Vector3 currentDamping; |
| | 13 | |
|
| | 14 | | private void Start() |
| | 15 | | { |
| 572 | 16 | | currentDamping = damping; |
| | 17 | |
|
| 572 | 18 | | if (target != null) |
| | 19 | | { |
| 572 | 20 | | transform.position = target.position; |
| 572 | 21 | | transform.forward = target.forward; |
| | 22 | | } |
| | 23 | |
|
| 572 | 24 | | transform.parent = null; |
| 572 | 25 | | } |
| | 26 | |
|
| | 27 | | private void OnEnable() |
| | 28 | | { |
| 597 | 29 | | CommonScriptableObjects.worldOffset.OnChange += OnWorldOffsetChange; |
| 597 | 30 | | } |
| | 31 | |
|
| | 32 | | private void OnDisable() |
| | 33 | | { |
| 597 | 34 | | CommonScriptableObjects.worldOffset.OnChange -= OnWorldOffsetChange; |
| 597 | 35 | | } |
| | 36 | |
|
| | 37 | | public void LateUpdate() |
| | 38 | | { |
| 6135 | 39 | | if ( target == null ) return; |
| | 40 | |
|
| 6135 | 41 | | Vector3 myPosition = transform.position; |
| 6135 | 42 | | Vector3 targetPosition = target.position; |
| 6135 | 43 | | Vector3 finalPosition = myPosition; |
| | 44 | |
|
| 6135 | 45 | | currentDamping += Damper.Damp( damping - currentDamping, Vector3.one * dampingChangeSpeed, Time.deltaTime); |
| 6135 | 46 | | finalPosition += Damper.Damp(targetPosition - myPosition, currentDamping, Time.deltaTime); |
| | 47 | |
|
| 6135 | 48 | | Transform t = this.transform; |
| 6135 | 49 | | t.position = finalPosition; |
| 6135 | 50 | | t.forward = target.forward; |
| 6135 | 51 | | } |
| | 52 | |
|
| | 53 | | private void OnWorldOffsetChange(Vector3 current, Vector3 previous) |
| | 54 | | { |
| 2 | 55 | | transform.position -= current - previous; |
| 2 | 56 | | } |
| | 57 | | } |