- Home /
Set direction of rotation C#
Hello. i am trying to make a spin wheel that is divided to 6 sections. Each section is marked with a gameObject that is centered in that section. after the player spins the wheel, it will rotate until its starts stopping and then the wheel moves and stops in the center based on the section that was selected (Randomly). i at using the following code to rotate the wheel towards the 0 on X axis. this code works fine and the wheel rotates fine, but only if the selected section was on the positive X axis.
float rotateFloat = (((randomReward + 1) * 60) - 30) - transform.rotation.z;
Quaternion targetRotation = Quaternion.Euler(new Vector3(0, 0, rotateFloat));
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, f_difX * Time.deltaTime);
i did some digging and found that Quaternion.RotateTowards()
finds the closest way towards the target and rotates using that direction (This caused a problem with the direction of rotation).
Example (Follow image): The player swipes and randomReward (Number 5 on spin wheel) and the wheel starts rotating when the wheel slows down, it starts moving towards the center. Then it will stop spinning along the direction of the swipe and will rotate towards the other side (Because the distance to the center is closer from the left side).
How can i set it to follow the same direction of the swipe regardless of which is closer to the center. And maybe if there is a better way to do this, please enlighten me. Thanks.
Answer by Glurth · Sep 17, 2017 at 04:57 PM
I think the issue stems from the fact that you specify the end point of the rotation (an orientation), rather than an amount to rotate.
When the player swipes left, specify a rotation of Euler(0,0,1).
When the player swipes right, specify a rotation of Euler(0,0,-1). (might have this exactly backwards)
THEN generate a random positive number, and multiply by the rotation specified above, to get a random amount of rotation, in that direction.
totalRot= MouseDirRot * randomNumber;
transform.rotatation= transform.rotatation * totalRot;// this operartion will add to the transform.rotatation, the rotation totalRot.
To animate, you will want to LERP that randomNumber from 0 to the selected random number, over time. I can see you're familiar with that, so I'll omit those details. (unless you ask about 'em!)
Your answer
![](https://koobas.hobune.stream/wayback/20220612135043im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Can't do Quaternion.AngleAxis twice. 1 Answer
Clamp an object on 2 sides 0 Answers
Look rotation viewing is zero warning 0 Answers