I'm trying to translate the roll(y) of a phone into the scale of an object but with size limits.
I have had success scaling to infinity +- with the following code:
void Update () {
float accelY = Input.acceleration.y;
transform.localScale += new Vector3(0, accelY, 0);
}
The problem I am having is with the max and min limits of the scale.
I attempted to use Clamp in the following way but have ended up just limiting the direction of scale where -y (rolling toward myself) equals 0 therefore no shrinking happens and +y (rolling away from self) scales up endlessly assumed at a max rate of 0.045f.
transform.localScale += new Vector3(0, Mathf.Clamp(accelY, 0f, 0.045f), 0);
In an ideal world I'd like to have a min scale of y = 1 and a max scale y = 100 and as you roll the phone back and forth it would tween between the two sizes.
I can find code snippets for Translate and rotate but nothing for scaling.. Please help.
void Update () {
float accelY = Input.acceleration.y;
transform.localScale = new Vector3(0, Mathf.Clamp(transform.localScale.y + accelY, 1f,
100f), 0)
}
You should add Time.deltaTime because Update is frames/sec dependant:
[SerializeField]
float speed = 10f;
[SerializeField]
float minSize = 1f;
[SerializeField]
float maxSize = 100f;
void Update () {
float accelY = Input.acceleration.y * Time.deltaTime * speed;
transform.localScale = new Vector3(0, Mathf.Clamp(transform.localScale.y + accelY, minSize ,
maxSize ), 0)
}