forked from RonenNess/UnityUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExMath.cs
More file actions
51 lines (48 loc) · 1.47 KB
/
ExMath.cs
File metadata and controls
51 lines (48 loc) · 1.47 KB
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
/**
* Misc math utils and extra functionality.
*
* Author: Ronen Ness.
* Since: 2017.
*/
using UnityEngine;
using System.Collections;
namespace NesScripts.Misc
{
/// <summary>
/// Extra math utilities.
/// </summary>
public class ExMath
{
/// <summary>
/// Get angle between two points, without Y axis.
/// </summary>
/// <param name="from">First point.</param>
/// <param name="to">Second point.</param>
/// <returns>Angle between points.</returns>
public static float AngleXZ(Vector3 from, Vector3 to)
{
Vector2 p1 = new Vector2(from.x, from.z);
Vector2 p2 = new Vector2(to.x, to.z);
return Angle(p1, p2);
}
/// <summary>
/// Get angle between two 2d points.
/// </summary>
/// <param name="p1">First point.</param>
/// <param name="p2">Second point.</param>
/// <returns>Angle between points.</returns>
public static float Angle(Vector2 p1, Vector2 p2)
{
return Mathf.Atan2(p2.y - p1.y, p2.x - p1.x) * 180 / Mathf.PI;
}
/// <summary>
/// Get avarage between two angles.
/// Note: this also represent the angle directly between two other angles.
/// </summary>
/// <returns>Avarage value between two angles.</returns>
public float AnglesAvg(float a, float b)
{
return Mathf.LerpAngle(a, b, 0.5f);
}
}
}