I have an interactable lever in my VR game. I'm using a hinge joint for the rotation and that's working very well. I'd like to use the X-axis rotation to send out a variable called "outputFloat." My biggest issue is that I need to convert the rotation of the lever to a 0 - 1 float. I've been messing with everything for hours but I just cannot get something to work. I'll attach my code below.
public class lever : MonoBehaviour { public Transform leverArm; public float min; public float max; public float outputFloat; void Update() { Mathf.Clamp(leverArm.localRotation.x, min, max); outputFloat = leverArm.localRotation.x * max; outputFloat = 1 - outputFloat; Debug.Log(outputFloat); } }
You know already that the max is 360, so wouldn't this work?:
void Update() { outputFloat = leverArm.localEulerAngles.x; outputFloat = 1 - Mathf.Abs(outputFloat/360); Debug.Log(outputFloat); }
2.1m questions
2.1m answers
60 comments
57.0k users