Home > Software engineering >  how to use slider to rotate the joint in unity
how to use slider to rotate the joint in unity

Time:12-18

i use the slider to change the joint angle, here is my code.

void Update()
    {

a = sliderx.GetComponent<slider>().value;
b = slidery.GetComponent<slider>().value;
headjoint.transform.Rotate(a, b, 0);
    }


and after I slide the bar, the joint rotates around and won't stop. like

a = value

and not

a = value

, the slider not give a definite number?

why? do I need to use eularangle?

when I use

headjoint.transform.rotation = Quaternion.Euler(a, b, 0); 

enter image description here

it's change the rotation angle of my object ,to 0,0,0 when running

anyideas?

CodePudding user response:

Rotate as the name suggests rotates the object about a given amount. If you call this constantly with the same value your object is rotated constantly about the same amount.

You rather want to set a rotation.

Your attempt

headjoint.transform.rotation = Quaternion.Euler(sliderX.value, sliderY.value, 0);

is actually close, yes. But by default Rotate is applied in local space. So if you want that same behavior you'd rather use

// First of all use the correct type and don't use GetComponent repeatedly
public Slider sliderX;
public Slider sliderY;

void Update()
{
    headjoint.transform.localRotation = Quaternion.Euler(sliderX.value, sliderY.value, 0);
}
  • Related