-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSmoothFollow.cs
More file actions
36 lines (31 loc) · 838 Bytes
/
SmoothFollow.cs
File metadata and controls
36 lines (31 loc) · 838 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using UnityEngine;
namespace Wrj
{
public class SmoothFollow : MonoBehaviour
{
[SerializeField]
private Transform followTarget = null;
[SerializeField]
private float smoothTime = 0.3F;
[SerializeField]
[Range(0f, 1f)]
private float distanceThreshold = .1f;
private Vector3 offset;
private Vector3 velocity = Vector3.zero;
public Transform Target => followTarget;
void Start()
{
if (followTarget == null) return;
offset = transform.position - followTarget.position;
}
void LateUpdate()
{
if (followTarget == null) return;
Vector3 targetPosition = followTarget.position + offset;
if (Vector3.Distance(transform.position, targetPosition) > distanceThreshold)
{
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
}
}
}
}