-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransformExtentions.cs
More file actions
127 lines (112 loc) · 4.86 KB
/
TransformExtentions.cs
File metadata and controls
127 lines (112 loc) · 4.86 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using UnityEngine;
namespace UnityForge
{
public static class TransformExtentions
{
/// <summary>
/// Rotates the transform so the forward vector points from target's current position.
/// </summary>
/// <param name="target">Object to point away from.</param>
public static void LookFrom(this Transform transform, Transform target)
{
transform.LookFrom(target.position);
}
/// <summary>
/// Rotates the transform so the forward vector points from target's current position.
/// </summary>
/// <param name="target">Object to point away from.</param>
/// <param name="worldUp">Vector specifying the upward direction.</param>
public static void LookFrom(this Transform transform, Transform target, Vector3 worldUp)
{
transform.LookFrom(target.position, worldUp);
}
/// <summary>
/// Rotates the transform so the forward vector points from worldPosition.
/// </summary>
/// <param name="worldPosition">Point to look at.</param>
public static void LookFrom(this Transform transform, Vector3 worldPosition)
{
transform.rotation = Quaternion.LookRotation(transform.position - worldPosition);
}
/// <summary>
/// Rotates the transform so the forward vector points from worldPosition.
/// </summary>
/// <param name="worldPosition">Point to look at.</param>
/// <param name="worldUp">Vector specifying the upward direction.</param>
public static void LookFrom(this Transform transform, Vector3 worldPosition, Vector3 worldUp)
{
transform.rotation = Quaternion.LookRotation(transform.position - worldPosition, worldUp);
}
/// <summary>
/// Sets the transform world space position x component.
/// </summary>
/// <param name="value">Value of x component.</param>
public static void SetPositionX(this Transform transform, float value)
{
transform.position = Vector3Utils.SetX(transform.position, value);
}
/// <summary>
/// Sets the transform world space position y component.
/// </summary>
/// <param name="value">Value of y component.</param>
public static void SetPositionY(this Transform transform, float value)
{
transform.position = Vector3Utils.SetY(transform.position, value);
}
/// <summary>
/// Sets the transform world space position z component.
/// </summary>
/// <param name="value">Value of z component.</param>
public static void SetPositionZ(this Transform transform, float value)
{
transform.position = Vector3Utils.SetZ(transform.position, value);
}
/// <summary>
/// Sets the transform local space position x component.
/// </summary>
/// <param name="value">Value of x component.</param>
public static void SetLocalPositionX(this Transform transform, float value)
{
transform.localPosition = Vector3Utils.SetX(transform.localPosition, value);
}
/// <summary>
/// Sets the transform local space position y component.
/// </summary>
/// <param name="value">Value of y component.</param>
public static void SetLocalPositionY(this Transform transform, float value)
{
transform.localPosition = Vector3Utils.SetY(transform.localPosition, value);
}
/// <summary>
/// Sets the transform world space position z component.
/// </summary>
/// <param name="value">Value of z component.</param>
public static void SetLocalPositionZ(this Transform transform, float value)
{
transform.localPosition = Vector3Utils.SetZ(transform.localPosition, value);
}
public static void AddChild(this Transform transform, GameObject childGameObject)
{
childGameObject.transform.SetParent(transform, false);
}
public static void AddChild(this Transform transform, GameObject childGameObject, bool worldPositionStays)
{
childGameObject.transform.SetParent(transform, worldPositionStays);
}
public static void AddChild(this Transform transform, Transform childTransform)
{
childTransform.SetParent(transform, false);
}
public static void AddChild(this Transform transform, Transform childTransform, bool worldPositionStays)
{
childTransform.SetParent(transform, worldPositionStays);
}
public static void DestroyChildren(this Transform transform)
{
for (var i = 0; i < transform.childCount; ++i)
{
Object.Destroy(transform.GetChild(i).gameObject);
}
}
}
}