Question by
nj4242 · Dec 09, 2016 at 12:15 PM ·
clampdragginglocalscalemathf.clampscaledown
MathF.clamp not working!
Hi,
I am scaling my game object on mouse dragging, and I want to restrict the scale values, but the objects doesnt stops scaling to min and max while I am dragging to scale it.
Code block :
void OnMouseDrag()
{
gameObject.transform.localScale = new Vector3(Mathf.Clamp(gameObject.transform.localScale.x, 2.8f, 7.2f), gameObject.transform.localScale.y, 1);
}
Comment
I'm not sure if i understand what you want to achieve. I wrote this code. I hope it is what you want.
using UnityEngine;
using System.Collections;
public class Game$$anonymous$$anager : $$anonymous$$onoBehaviour {
float prev$$anonymous$$ousex = 0f;
private void Start()
{
prev$$anonymous$$ousex = Input.mousePosition.x;
}
private void On$$anonymous$$ouseDrag()
{
if (prev$$anonymous$$ousex != Input.mousePosition.x) // if mouse changed position. Simply if dragging...
{
float changeAmountx = Input.mousePosition.x - prev$$anonymous$$ousex;
gameObject.transform.localScale = new Vector3($$anonymous$$athf.Clamp(gameObject.transform.localScale.x + (changeAmountx * 0.1f), 2.8f, 7f), gameObject.transform.localScale.y, 1);
prev$$anonymous$$ousex = Input.mousePosition.x;
}
}
}
I want to restrict scaling with a $$anonymous$$imum and maximum value supplied to mathf.clamp, I have nothing to do with mouse positions, I just to restrict scale values, either with clamping or any other work around. Thanks for answering.
Answer by Content1of_akind · Oct 07, 2020 at 12:32 AM
Your code helped me figure out what we both needed, hope this helps somebody else, cheers!
[SerializeField]
private float xMin, xMax, yMin, yMax
transform.position = new Vector3(Mathf.Clamp(this.transform.position.x, xMin, xMax), Mathf.Clamp(this.transform.position.y, yMin, yMax),0);
Your answer
![](https://koobas.hobune.stream/wayback/20220612102348im_/https://answers.unity.com/themes/thub/images/avi.jpg)