summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/SmoothCameraFollow.cs
blob: 80efdfb83e55cfc96b597ee9e7b7c3a4711d0bb7 (plain)
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
using UnityEngine;

namespace Pathfinding.Examples {
	/// <summary>
	/// Smooth Camera Following.
	/// \author http://wiki.unity3d.com/index.php/SmoothFollow2
	/// </summary>
	[HelpURL("https://arongranberg.com/astar/documentation/stable/smoothcamerafollow.html")]
	public class SmoothCameraFollow : VersionedMonoBehaviour {
		public Transform target;
		public float distance = 3.0f;
		public float height = 3.0f;
		public float damping = 5.0f;
		public bool enableRotation = true;
		public bool smoothRotation = true;
		public float rotationDamping = 10.0f;
		public bool staticOffset = false;

		void LateUpdate () {
			Vector3 wantedPosition;

			if (staticOffset) {
				wantedPosition = target.position + new Vector3(0, height, distance);
			} else {
				wantedPosition = target.TransformPoint(0, height, -distance);
			}
			transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);

			if (enableRotation) {
				if (smoothRotation) {
					Quaternion wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
					transform.rotation = Quaternion.Slerp(transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
				} else transform.LookAt(target, target.up);
			}
		}
	}
}