Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
69 views
in Technique[技术] by (71.8m points)

c# - How to convert rotation to 0 - 1 float

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);
    }

}
question from:https://stackoverflow.com/questions/65862084/how-to-convert-rotation-to-0-1-float

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...