[solved ]UI rotation via button click
Hey, I try to rotate an Image when I click on a button each time 6°. I tried this:
transform.rotation = Quaternion.Angle(6 * Time.deltaTime, Vector3.forward);
but it caused wierd spinning issues were it was slow in the beginnung but got faster with each click (even with Time.deltaTime?!)
using UnityEngine;
using UnityEngine.UI;
public class Rotator : MonoBehaviour
{
public Button Clockwise, CClockwise;
public Toggle Wheel;
public Vector3 startRot;
public Vector3 endRot;
public void RotPositive() // do I need to remove the Listener?
{
Clockwise.onClick.AddListener(PosWheel);
}
public void RotNegative()
{
CClockwise.onClick.AddListener(NegWheel);
}
public void PosWheel()
{
if (Wheel.isOn)
{
endRot = new Vector3 (startRot.x, startRot.y, startRot.z + 6);
transform.rotation = Quaternion.FromToRotation(startRot, endRot);
startRot = transform.rotation.eulerAngles;
}
}
public void NegWheel()
{
if (Wheel.isOn)
{
transform.rotation = Quaternion.AngleAxis(-6, startRot);
startRot = transform.rotation.eulerAngles;
}
}
now the rotation is aplied once no matter what method is called and further button clicks have no impact at all.. please help :P
Comment
Answer by lolesch · Nov 11, 2018 at 01:06 PM
I finally found it myself!
using UnityEngine;
using UnityEngine.UI;
public class Rotate : MonoBehaviour
{
public Button Clockwise, CClockwise;
public Toggle Wheel;
public int degree = 5;
private int sum;
public void Update()
{
if (Mathf.Abs(sum) >= 360)
{
sum = 0;
}
}
public void RotPositive()
{
if (Wheel.isOn)
{
sum -= degree;
transform.rotation = Quaternion.AngleAxis(sum, Vector3.forward);
}
}
public void RotNegative()
{
if (Wheel.isOn)
{
sum += degree;
transform.rotation = Quaternion.AngleAxis(sum, Vector3.forward);
}
}
}