I need to rotate objects on the x, y, and z axis but my objects are not rotating on the y and z axis. I am using one slider with to rotate the objects in the x plane but now wanted to rotate the object in y and z too. So what changes should be made to the existing code to make the objects rotate in the y and z planes?
using System;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class RotateObject : MonoBehaviour
{
// Assign in the inspector
private GameObject objectToRotate;
public Slider slider;
// Preserve the original and current orientation
private float previousValue;
private float startValue = 0.5f;
void Awake ()
{
// Assign a callback for when this slider changes
slider.onValueChanged.AddListener (OnSliderChanged);
// And current value
previousValue = slider.value;
}
void OnSliderChanged (float value)
{
// How much we've changed
float delta = value - previousValue;
objectToRotate.transform.Rotate (Vector3.up * delta * 360);
// Set our previous value for the next change
previousValue = value;
}
void Update()
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
Ray ray = Camera.current.ScreenPointToRay(touch.position);
RaycastHit hitObject;
if (Physics.Raycast(ray, out hitObject))
{
objectToRotate = hitObject.transform.parent.transform.parent.gameObject;
objectToRotate.GetComponent<Recolour>().SetSelected();
}
}
}
public void Deselect()
{
objectToRotate.GetComponent<Recolour>().SetOriginalMaterial();
objectToRotate = null;
slider.value = startValue;
}
}
CodePudding user response:
Add two more references to the other sliders, two previousValue
variables and OnSliderChanged
functions.
public Slider sliderX, sliderY, sliderZ;
private float previousValueX, previousValueY, previousValueZ;
void Awake ()
{
// Assign a callback for when this slider changes
sliderX.onValueChanged.AddListener (OnSliderChangedX);
sliderY.onValueChanged.AddListener (OnSliderChangedY);
sliderZ.onValueChanged.AddListener (OnSliderChangedZ);
// And current value
previousValueX = sliderX.value;
previousValueY = sliderY.value;
previousValueZ = sliderZ.value;
}
void OnSliderChangedX (float value)
{
// How much we've changed
float delta = value - previousValueX;
objectToRotate.transform.Rotate (Vector3.right * delta * 360);
// Set our previous value for the next change
previousValueX = value;
}
void OnSliderChangedY (float value)
{
// How much we've changed
float delta = value - previousValueY;
objectToRotate.transform.Rotate (Vector3.up * delta * 360);
// Set our previous value for the next change
previousValueY = value;
}
void OnSliderChangedZ (float value)
{
// How much we've changed
float delta = value - previousValueZ;
objectToRotate.transform.Rotate (Vector3.forward * delta * 360);
// Set our previous value for the next change
previousValueZ = value;
}
public void Deselect()
{
objectToRotate.GetComponent<Recolour>().SetOriginalMaterial();
objectToRotate = null;
sliderX.value = startValue;
sliderY.value = startValue;
sliderZ.value = startValue;
}