Reputation: 14141
I want to rotate an Image 90º with every button click. My problem is when I clicked it only turn to exactly 90º. When I click again it does not go to 180º, 270º and back to 0º on the forth click.
public Image image;
public void WheelClicker()
{
image.gameObject.transform.DORotate(new Vector3(0, 0, image.gameObject.transform.rotation.z + 90), 1);
}
Upvotes: 2
Views: 1708
Reputation: 90580
transform.rotation
is a Quaternion
value! unless you know exactly what you are doing you should never touch its individual components (x
,y
,z
,w
). The value transform.rotation.z
is not the value you are looking for!
You should rather use the transform.eulerAngles
public Image image;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space)) WheelClicker();
}
public void WheelClicker()
{
// always prevent concurrent animations
if (DOTween.IsTweening(image.transform)) return;
image.transform.DORotate(image.transform.eulerAngles + new Vector3(0, 0, 90), 1);
}
Upvotes: 4
Reputation: 106
this two work for me:
GameObject go;
float someValue = 0;
void Update()
{
someValue += 1;
go.transform.eulerAngles = new Vector3(someValue, 0, 0);
}
and this one:
GameObject go;
void Update()
{
go.transform.Rotate(0, 90, 0, Space.World);
}
so try maybe something like this in your code:
public Image image;
int someValue = 0;
public void WheelClicker()
{
someValue += 90;
image.gameObject.transform.eulerAngles(new Vector3(0, 0,someValue ), 1);
}
or:
public Image image;
public void WheelClicker()
{
image.gameObject.transform.Rotate(new Vector3(0, 0, 90), 1);
}
Upvotes: 1
Reputation: 86
From the DOTween docs (under "Rigidbody"), it seems that you need to use either WorldSpaceAdd or LocalSpaceAdd as your rotation mode. Then, you write something like: gameObject.transform.DORotate(new Vector3(0, 0, 90), 1, RotateMode.WorldAxisAdd).
Just make sure the user can't trigger a rotation during a rotation, potentially knocking you off your 90/180/270 course. The simplest way I can think to do this is:
if(!DOTween.IsTweening(transform) && Input.GetKeyUp("space"))
{
transform.DORotate(new Vector3(0, 0, 90), 1, RotateMode.WorldAxisAdd);
}
In the case that pressing space would trigger the tween.
Upvotes: 2