-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMoveTransformCross.cs
67 lines (53 loc) · 1.99 KB
/
MoveTransformCross.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
using UnityEngine;
namespace HiddenUnits
{
public class MoveTransformCross : MonoBehaviour
{
private void Start()
{
var teamHolder = GetComponent<TeamHolder>();
if (teamHolder && teamHolder.spawnerWeapon)
{
Target = teamHolder.spawnerWeapon.transform;
}
else
{
TrackedPosition = transform.position;
}
}
private void Update()
{
Counter += Time.deltaTime;
if (updatePosition && Target != null)
{
TrackedPosition = Target.transform.position;
}
var direction = (TrackedPosition - transform.position).normalized;
direction.y = 0f;
var rotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.RotateTowards(transform.rotation, rotation,
rotationSpeedOverTime.Evaluate(Counter) * rotationForce * Time.deltaTime);
projectile.velocity += transform.forward * (force * Time.deltaTime);
projectile.velocity -= projectile.velocity * (drag * Time.deltaTime);
if (Counter > returnDelay && Vector3.Distance(transform.position, TrackedPosition) < returnThreshold)
{
if (Target != null) Target.GetComponent<DelayEvent>().Go();
delayEvent.Go();
}
}
private float Counter;
private Transform Target;
private Vector3 TrackedPosition;
public bool updatePosition;
public MoveTransform projectile;
public DelayEvent delayEvent;
public AnimationCurve rotationSpeedOverTime;
[Header("Movement")]
public float force = 1f;
public float rotationForce = 1f;
public float drag = 0.9f;
[Header("Return")]
public float returnThreshold = 0.2f;
public float returnDelay = 2f;
}
}