-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLerpPosInterval.cs
61 lines (53 loc) · 2.2 KB
/
LerpPosInterval.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
using System.Collections;
using UnityEngine;
namespace UnityLerpCoroutines
{
// The LerpPosInterval class
// Smoothly interpolates the items positon from what it currently is to pos
public class LerpPosInterval : LerpInterval
{
public Vector3 lerpTo;
private float Lerpx, Lerpy, Lerpz;
public LerpPosInterval(GameObject gameObject, Vector3 pos, float time, BlendTypes blend = BlendTypes.None,
TransformType transformType = TransformType.local)
{
BlendType = blend;
this.gameObject = gameObject;
lerpTo = pos;
this.time = time;
this.transformType = transformType;
}
public override string Print()
{
return "LerpPosInterval(" + gameObject.name + ", " + lerpTo + ", " + time;
}
public override IEnumerator RunInterval(float t = 0)
{
Vector3 startPos;
timeElapsed = 0f;
if (transformType == TransformType.local)
startPos = gameObject.transform.localPosition;
else
startPos = gameObject.transform.position;
while (timeElapsed < time)
{
var step = getLerpStep();
Lerpx = Mathf.Lerp(startPos.x, lerpTo.x, step);
Lerpy = Mathf.Lerp(startPos.y, lerpTo.y, step);
Lerpz = Mathf.Lerp(startPos.z, lerpTo.z, step);
timeElapsed += Time.deltaTime;
if (transformType == TransformType.local)
gameObject.transform.localPosition = new Vector3(Lerpx, Lerpy, Lerpz);
else if (transformType == TransformType.RigidBody2D)
gameObject.GetComponent<Rigidbody2D>().MovePosition(new Vector3(Lerpx, Lerpy, Lerpz));
else
gameObject.transform.position = new Vector3(Lerpx, Lerpy, Lerpz);
yield return timeElapsed;
}
if (transformType == TransformType.local)
gameObject.transform.localPosition = lerpTo;
else
gameObject.transform.position = lerpTo;
}
}
}