forked from RonenNess/UnityUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFollowObject.cs
More file actions
64 lines (52 loc) · 2.12 KB
/
Copy pathFollowObject.cs
File metadata and controls
64 lines (52 loc) · 2.12 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Makes object follow another object while looking at it.
* Useful as a top-down camera controller or following effects (like a cloud above the player etc).
* Author: Ronen Ness.
* Since: 2017.
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace NesScripts.Controls
{
/// <summary>
/// Make the object follow another object and look at it, with optional offsets and smooth movement.
/// </summary>
public class FollowObject : MonoBehaviour
{
// target to follow.
public GameObject Target;
// position offset from target.
public Vector3 PositionOffset = new Vector3(0, 0, 0);
// lookat offset from target.
public Vector3 LookatOffset = new Vector3(0, 0, 0);
// if true, will rotate to lookat target (turn to false to disable rotation)
public bool ControlRotation = true;
// if true, will move to target position (turn to false to disable movement)
public bool ControlPosition = true;
// if > 0.0f, will follow / lookat target with damping, eg smoothly
public float DampingTime = 0.25f;
// movement velocity for damping
Vector3 moveVelocity = new Vector3(0, 0, 0);
// update the follower
void Update()
{
// control position
if (ControlPosition)
{
// get target position
var targetPos = Target.transform.position + PositionOffset;
// move to target
transform.position = DampingTime > 0 ? Vector3.SmoothDamp(transform.position, targetPos, ref moveVelocity, DampingTime) : targetPos;
}
// control rotation
if (ControlRotation)
{
// get target rotation
var targetRotation = Quaternion.LookRotation((Target.transform.position + LookatOffset) - transform.position );
// rotate to look at target
transform.rotation = DampingTime > 0 ? Quaternion.Slerp(transform.rotation, targetRotation, (1f / DampingTime) * Time.deltaTime) : targetRotation;
}
}
}
}