- Home /
Problem with "if or" statement.
I have a small and simple script that makes something spin, well I got it to spin, my problem now is getting it to spin and stop at a certain rotation in degrees. Currently it spins as long as the rotation value is > or < the target (depending on which direction it's spinning) however, that doesn't work nicely if it's at 0, in which case I can't have it spin one direction due to it not > the target number. So I added a "|| rotation1 == 0" so that way it would spin even if it were 0, but now it stops when it's not 0. What am I doing wrong here?
function Activate (end, direction) {
if (end == 1 && direction == 1) {
if (rotation1 > 90 || rotation1 == 0) {
Spin(1);
}
}
}
What is rotation1 ?
if (end == 1 && direction == 1) {
Debug.Log( "rotation1 = " + rotation1 );
if (rotation1 > 90 || rotation1 == 0) {
rotation1 is the current rotation value for the spinning object. The combination of "end" and "direction" indicate a combination of rotation1's goal (what it should stop at when it gets to) and "Spin" which indicates the direction (left or right) to rotate. Again, the problem is that my OR statement indicates to spin if it's equal to 0 or greater than 90, but it stops after no longer being equal to 0.
If it's 0, it will spin a little. Let's say it spins to 0.01. Now it's not 0 and it's not > 90. I think you will find this helpful: http://wiki.unity3d.com/index.php/Angle
Answer by Vonni · May 08, 2013 at 06:39 PM
I had the same problems trying to position and rotate things over time and used if statements all over the place, but it simply doesnt work well.
Use Mathf.Lerp (Read up on this) to do presision positioning/rotating that have start and end positions at some point
Or Something using the link DaveA posted.
Sidenote Lets say you rotate a ball around and want it to stop at 50 degree rotation, Doing if(degree == 50) will pretty much never work as its value will almost always be 49.2044, 49.7094, 50.1235 etc. and > 50 will not give you accurate rotations.
Also remember that as soon as the euler goes over 360 its starts back at 0.
Your answer
Follow this Question
Related Questions
Rotation: rotate by a defined angle 1 Answer
Rotate object in 45 degree steps 1 Answer
How can I rotate an object with certain degrees? 2 Answers
What is the most efficient way to rotate an object? 3 Answers
Detecting the range between 2 angles 2 Answers