-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleMover.cs
82 lines (65 loc) · 2.23 KB
/
SimpleMover.cs
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using MyBox;
using UnityEngine;
using UnityEngine.Events;
/// <summary>
/// Moves the parent object around with smooth easing.
/// Can be ordered to start and stop.
/// Invokes unity events when movement starts or stops
/// </summary>
public class SimpleMover : MonoBehaviour {
[Tooltip("The maximum speed the object can move at.")]
public float maxMoveSpeed = 5f;
[Tooltip("The rate at which the object speeds up or slows down.")]
public float acceleration = 2f;
[Tooltip("The direction the object will move in.")]
public MoveDirection moveDirection;
[Tooltip("Should the object move automatically on start?")]
public bool MoveOnStart = false;
[Tooltip("Event triggered when the object starts moving.")]
public UnityEvent onStartMovement;
[Tooltip("Event triggered when the object stops moving.")]
public UnityEvent onStopMovement;
private float currentSpeed = 0f;
private bool isMoving = false;
public enum MoveDirection { Forward, Left, Back, Down }
private void Start() {
if (MoveOnStart)
{
StartMovement();
}
}
void Update() {
HandleMovement();
}
public void StartMovement() {
isMoving = true;
onStartMovement.Invoke();
}
public void StopMovement() {
isMoving = false;
onStopMovement.Invoke();
}
private void HandleMovement() {
if (isMoving) {
currentSpeed = Mathf.Min(maxMoveSpeed, currentSpeed + acceleration * Time.deltaTime);
} else {
currentSpeed = Mathf.Max(0, currentSpeed - acceleration * Time.deltaTime);
}
if (currentSpeed > 0) {
Vector3 direction = GetDirection();
transform.Translate(direction * currentSpeed * Time.deltaTime);
}
}
private Vector3 GetDirection() {
switch (moveDirection) {
case MoveDirection.Left:
return Vector3.left;
case MoveDirection.Back:
return Vector3.back;
case MoveDirection.Down:
return Vector3.down;
default:
return Vector3.forward;
}
}
}