Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3de08b7

Browse files
committed
implement own Lerp function
1 parent 4f691aa commit 3de08b7

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

‎Assets/Line.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections;
22
using System.Collections.Generic;
3+
using Unity.VisualScripting;
34
using UnityEngine;
45

56
public class Line
@@ -8,15 +9,32 @@ public class Line
89
public Vector3 B;
910
public Vector3 v;
1011

11-
public Line(Vector3 A, Vector3 B)
12+
public enum LINETYPE { LINE, SEGMENT, RAY };
13+
LINETYPE type;
14+
15+
public Line(Vector3 A, Vector3 B, LINETYPE type)
1216
{
1317
this.A = A;
1418
this.B = B;
1519
this.v = B - A;
20+
this.type = type;
1621
}
1722

18-
public Vector3 GetPointAt(float t)
23+
public Vector3 Lerp(float t)
1924
{
25+
if (type == LINETYPE.LINE)
26+
{
27+
t = Mathf.Clamp(t, float.MinValue, float.MaxValue);
28+
}
29+
else if (type == LINETYPE.SEGMENT)
30+
{
31+
t = Mathf.Clamp01(t);
32+
}
33+
else if (type == LINETYPE.RAY)
34+
{
35+
t = Mathf.Clamp(t, 0, float.MaxValue);
36+
}
37+
2038
return A + (v * t);
2139
}
2240
}

‎Assets/Move.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ public class Move : MonoBehaviour
1010

1111
void Start()
1212
{
13-
line = new Line(start.position, end.position);
13+
line = new Line(start.position, end.position,Line.LINETYPE.SEGMENT);
1414
}
1515

1616
void Update()
1717
{
18-
transform.position = line.GetPointAt(Time.time);
18+
transform.position = UMath.Lerp(start.position,end.position,Time.time*0.05f);
1919
}
2020
}

‎Assets/Scenes/CartesianPlayground/UMath.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,10 @@ static public Vector3 RotateVector(Vector3 vector, float angle, bool clockWise)
6767

6868
return new Vector3(xVal, yVal, 0);
6969
}
70+
71+
static public Vector3 Lerp(Vector3 a, Vector3 b, float t)
72+
{
73+
Vector3 v = b - a;
74+
return a + (v * Mathf.Clamp01(t));
75+
}
7076
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /