Skip to content

Commit f31a532

Browse files
committed
Added scale extension methods for Vector2/Vector3
1 parent adc6603 commit f31a532

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed

Runtime/Extensions/Unity/Vector2Extensions.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,42 @@ public static Vector2 Rotate(this Vector2 source, float rotationAmount, bool use
6161
new Vector2(rotatedX, rotatedY);
6262
}
6363

64+
/// <summary>
65+
/// Returns the <see cref="Vector2"/> with a set <paramref name="x"/> component, and Y-component
66+
/// scaled to match while retaining the X,Y proportions of the source <see cref="Vector2"/>.
67+
/// </summary>
68+
/// <param name="x">
69+
/// A X-component value for the scaled <see cref="Vector2"/>.
70+
/// </param>
71+
/// <returns>
72+
/// A <see cref="Vector2"/>.
73+
/// </returns>
74+
public static Vector2 ScaledToX(this Vector2 source, float x)
75+
{
76+
var factor = x / source.x;
77+
var y = source.y * factor;
78+
return
79+
new(x, y);
80+
}
81+
82+
/// <summary>
83+
/// Returns the <see cref="Vector2"/> with a set <paramref name="y"/> component, and X-component
84+
/// scaled to match while retaining the X,Y proportions of the source <see cref="Vector2"/>.
85+
/// </summary>
86+
/// <param name="y">
87+
/// A Y-component value for the scaled <see cref="Vector2"/>.
88+
/// </param>
89+
/// <returns>
90+
/// A <see cref="Vector2"/>.
91+
/// </returns>
92+
public static Vector2 ScaledToY(this Vector2 source, float y)
93+
{
94+
var factor = y / source.y;
95+
var x = source.x * factor;
96+
return
97+
new(x, y);
98+
}
99+
64100
/// <summary>
65101
/// Converts a <see cref="Vector2"/>[] into a <see cref="Vector3"/>[].
66102
/// </summary>

Runtime/Extensions/Unity/Vector3Extensions.cs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static float DistanceTo(this Vector3 source, Vector3 destination)
3131
}
3232

3333
/// <summary>
34-
/// Return the <see cref="Vector3"/> with its Y-component to 0.
34+
/// Return the <see cref="Vector3"/> with its Y-component at 0.
3535
/// </summary>
3636
/// <returns>A modified <see cref="Vector3"/> with y component set to 0.</returns>
3737
public static Vector3 Flat(this Vector3 source)
@@ -40,6 +40,63 @@ public static Vector3 Flat(this Vector3 source)
4040
new Vector3(source.x, 0, source.z);
4141
}
4242

43+
/// <summary>
44+
/// Returns the <see cref="Vector3"/> with a set <paramref name="x"/> component, and Y- and Z-components
45+
/// scaled to match while retaining the X,Y,Z proportions of the source <see cref="Vector3"/>.
46+
/// </summary>
47+
/// <param name="x">
48+
/// A X-component value for the scaled <see cref="Vector3"/>.
49+
/// </param>
50+
/// <returns>
51+
/// A <see cref="Vector3"/>.
52+
/// </returns>
53+
public static Vector3 ScaledToX(this Vector3 source, float x)
54+
{
55+
var factor = x / source.x;
56+
var y = source.y * factor;
57+
var z = source.z * factor;
58+
return
59+
new(x, y, z);
60+
}
61+
62+
/// <summary>
63+
/// Returns the <see cref="Vector3"/> with a set <paramref name="y"/> component, and X- and Z-components
64+
/// scaled to match while retaining the X,Y,Z proportions of the source <see cref="Vector3"/>.
65+
/// </summary>
66+
/// <param name="y">
67+
/// A Y-component value for the scaled <see cref="Vector3"/>.
68+
/// </param>
69+
/// <returns>
70+
/// A <see cref="Vector3"/>.
71+
/// </returns>
72+
public static Vector3 ScaledToY(this Vector3 source, float y)
73+
{
74+
var factor = y / source.y;
75+
var x = source.x * factor;
76+
var z = source.z * factor;
77+
return
78+
new(x, y, z);
79+
}
80+
81+
/// <summary>
82+
/// Returns the <see cref="Vector3"/> with a set <paramref name="z"/> component, and X- and Y-components
83+
/// scaled to match while retaining the X,Y,Z proportions of the source <see cref="Vector3"/>.
84+
/// </summary>
85+
/// <param name="z">
86+
/// A Z-component value for the scaled <see cref="Vector3"/>.
87+
/// </param>
88+
/// <returns>
89+
/// A <see cref="Vector3"/>.
90+
/// </returns>
91+
public static Vector3 ScaledToZ(this Vector3 source, float z)
92+
{
93+
var factor = z / source.z;
94+
var x = source.x * factor;
95+
var y = source.y * factor;
96+
return
97+
new(x, y, z);
98+
}
99+
43100
/// <summary>
44101
/// Converts a <see cref="Vector3"/>[] into a <see cref="Vector2"/>[].
45102
/// </summary>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.stephanhooft.unity-tools",
3-
"version": "7.4.2",
3+
"version": "7.5.0",
44
"displayName": "Unity Tools",
55
"description": "A Unity package for code to reuse across projects.",
66
"unity": "2021.3",

0 commit comments

Comments
 (0)