- Home /
Question by
unityplease · Jun 04, 2015 at 10:26 AM ·
rotationuislider
Rotate Object on single Axis (Slider UI)
I want to use two sliders to rotate an object in view in x and y.
The following code 'sort-of' works but occasionally one of the sliders causes the object to counter rotate and 'spazz' out.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Rotations : MonoBehaviour
{
public Slider xSlider;
public Slider ySlider;
public GameObject theTarget;
void Start ()
{
GetXRotation ();
GetYRotation ();
}
public void GetXRotation ()
{
xSlider.value = theTarget.transform.eulerAngles.x;
}
public void SetXRotation ()
{
Vector3 tempRotation = theTarget.transform.eulerAngles;
tempRotation.x = xSlider.value;
theTarget.transform.rotation = Quaternion.Euler (tempRotation);
}
public void GetYRotation ()
{
ySlider.value = theTarget.transform.eulerAngles.y;
}
public void SetYRotation ()
{
Vector3 tempRotation = theTarget.transform.eulerAngles;
tempRotation.y = ySlider.value;
theTarget.transform.rotation = Quaternion.Euler (tempRotation);
}
}
I use the script on two separate sliders but only address the SetYRotation or SetXRotation for each appropriate slider on value changed function.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613200625im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Controlling an objects axis with UI Slider 1 Answer
Minimap arrows 0 Answers
Rotation with a slider 2 Answers
Change the Shape Unity's UI Sliders 1 Answer
UI Slider - making the touch area for slide larger than the UI image handle 2 Answers