- Home /
restrict scaling
Hi,
I want to restrict scaling while I am dragging my gameobject in both X & Y axis. Hence, I will not be allowed to go less than scale "1" on both sides, and also not more than 5 scale value, to restrict in my game scene. What workaround should be beneficial to do that?
Thanks.
Answer by TylerMorton · Dec 09, 2016 at 09:28 PM
You can use a C# extension method to clamp your Vector3 scale between two other vectors. This makes the syntax look nice and clean in your main script!
public static class Vector3Extensions { public static void Clamp(this Vector3 vector, Vector3 min, Vector3 max) { // max check if (vector.x > max.x) vector.x = max.x; if (vector.y > max.y) vector.y = max.y; if (vector.z > max.z) vector.z = max.z; // min check if (vector.x < min.x) vector.x = min.x; if (vector.y < min.y) vector.y = min.y; if (vector.z < min.z) vector.z = min.z; } }
And you would use it like this:
this.transform.localscale.Clamp(new Vector3(1,1,1), new Vector3(5,5,5));
Answer by RobAnthem · Dec 09, 2016 at 09:03 PM
Hmmm, I'd create a script that attaches to the object, or a Editor tool, that essentially resizes it for your, alternatively you could have an Update script that checks the scale of the object and if it is greater than x, it = x, less than y, it = y.
EDIT: I took a look at Vector3, which I should of in the past, its super easy to get the metas by right clicking something in VS and clicking "Go to Definition"; I found this;
//
// Summary:
// ///
// Returns a copy of vector with its magnitude clamped to maxLength.
// ///
//
// Parameters:
// vector:
//
// maxLength:
public static Vector3 ClampMagnitude(Vector3 vector, float maxLength);