Emulating a physical dial


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,

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…

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;

  if(currentRotation < -100)
    rigidbody.transform.eulerAngles.z = -100;
  else if(currentRotation > 100)
    rigidbody.transform.eulerAngles.z = 100;