- Home /
Emulating a physical dial
Hello,
I am trying to replicate a physical dial/potentiometer like that which would be found on a synthesizer. So far I am able to rotate a rigidbody cylinder around freely on its x-axis with the z and y locked using the 'drag rigid body' script, but I need to limit its rotation to something like -45 to 45, so it has a 'min' and 'max' value which it can not exceed instead of spinning endlessly.
ANY help or advice would be appreciated, thanks
Answer by Meltdown · Jun 23, 2011 at 01:39 PM
You can do something something simple in your script like this to limit the rotation...
float currentRotation;
void FixedUpdate()
{
currentRotation = rigidbody.transform.eulerAngles.x;
if(currentRotation < -45)
rigidbody.transform.eulerAngles.x = -45;
else if(currentRotation > 45)
rigidbody.transform.eulerAngles.x = 45;
}
this code is not tested but you get the idea...
You can also use $$anonymous$$athf.Clamp
for the same thing (but in fewer lines).
Thanks for the script $$anonymous$$eltdown, and cheers for the suggestion Ben. I haven't had a chance to try either yet but I'll let you know as soon as I do. Thanks again!
Answer by operand · Jun 27, 2011 at 01:03 PM
I'm using the script you posted Meltdown (with javascript modifications) and the lower boundary works, but as the cylinder rotates back up to 0 it just keeps snapping to -100. So basically I can only rotate my dial left, then it keeps snapping back to the lower boundary value. The script looks like this.
var currentRotation : float;
function FixedUpdate(){
currentRotation = rigidbody.transform.eulerAngles.z;
Debug.Log(currentRotation);
if(currentRotation < -100)
rigidbody.transform.eulerAngles.z = -100;
else if(currentRotation > 100)
rigidbody.transform.eulerAngles.z = 100;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612115306im_/https://answers.unity.com/themes/thub/images/avi.jpg)