last day (14 days later) » 

3:15 AM
0
Q: How to stop rotation every 90 degrees?

alaslipknotI was facing another rotation problem yesterday, that i managed to solve by rotating the object like this : transform.Rotate (speed * Time.deltaTime, 0, 0, Space.World); I tried almost every other way but nothing seems to work, usually i only use Euler angles to rotate my objects but in this ...

 
I think you need if (time >= 1/60 ) instead of if (time > 0)
 
not really, beside even if that work (which it doesn't i just tested it) it meant to make me avoid the "manual adjustment" of the rotation inside the else statement, which is the purpose of this question, i need to know my target rotation before doing transform.Rotate();
 
could you explain what "didn't work" means ? you got the same result ? could you try if (time >= 1/60 + 0.01f) ?
 
yes i tried it, when i keep the 'transform.rotation = Quaternion.Euler (targetEul);' inside the else the statement i get the same result as the 2nd GIF preview, when i remove that, i get the same result as the first GIF preview but only a bit "later"
 
i never said you should remove the else statement. It is needed. Also, shouldn't targetEul.x += 90; be targetEul.z += 90; instead ? you seem to want to rotate according to Z axis here: transform.Rotate (0, 0, speed * Time.deltaTime, Space.World);
 
3:15 AM
yeah that's why i said both, i tried with and without the else statement and the result is pretty much the same, for the other remark, you are right, the example i wrote above wasn't precise but in my class am sure am using the correct axes, i edited the original post and added the entire class.
 
hello
 
hi
sorry for the delay i was away
 
so, i edited your code in your question
those white spaces were terrible.... :p
 
lol
and i was looking for a "real" editing 8-)
 
is your game running in 60 fps ?
 
3:25 AM
minimum : 79fps
max : 100fps
(in editor)
but
you are using the wrong approach to solve the problem
forget about time
 
why is your game's fps not running in stable fps ?
if (timer>0){
timer -= speed * Time.deltaTime;
transform.Rotate (0, 0, speed * Time.deltaTime, Space.World);
}else{
transform.rotation = Quaternion.Euler (targetEul);
}
 
my problem is here : transform.rotation = Quaternion.Euler (targetEul);
forget about time and fps please
these are not relevant
 
alright
so what is the problem ?
according to you
 
see when i press one of the arrow key, i assumed that my final rotation would be this !
:*
targetQuat = Quaternion.Euler (transform.rotation.eulerAngles.x + 90, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);
 
thats the left key
according to your code
the right key does the same thing for Z axis
am i right ?
 
3:29 AM
yeah i know (i didn't mention which one, it doesn't matter :p )
 
ok so we have
    targetEul = transform.eulerAngles;
    targetEul.x += 90;
and then
if (chronoR > 0) {
    chronoR -= speed * Time.deltaTime;
    transform.Rotate (0, 0, -speed * Time.deltaTime, Space.World);
    myEul.z += speed * Time.deltaTime;
} else {
    transform.rotation = Quaternion.Euler (targetEul);
    //transform.rotation = Quaternion.Slerp (transform.rotation, targetQuat, 100);
    canRotate = true;
    doRight = false;
}
 
yeah i was using that earlier too
also give the same result
 
im just pasting your code
80% of the code in your question is irrelevant
trying to understand which parts you are having problem with
 
this one :
targetEul = transform.eulerAngles;
targetEul.x += 90;
and this one :
transform.rotation = Quaternion.Euler (targetEul);
i am assuming that there is some sort of conversion is being made between Quaternion and Euler
that's why i got that weird flipping
 
this code seems correct
the weird flipping is because
you rotate it more than what you should have
and then the "else" code takes place
where it reverts it to the position it should have been at max
 
3:33 AM
wrong
"you rotate it more than what you should have"
even if that is true (which it is)
i am saving the correct target rotation before even start rotating
am saving it right when i press the key
 
i know
 
so even if i rotate it more than it should
the else should fix it
 
nope
and i will explain why
what you say
would happen
if you had
if()...
if()...

you have

if()
else
:)
 
huh ?
why ?
if (chronoR > 0)
 
do you understand the difference of if/else and if/if ?
 
3:36 AM
will eventuall become false
and execute its else no ?
yes of course
else will be executed only when the if is false
and that's what i need
 
ok sorry that my english sucks right now, let me break it down for you
 
if(x != 1)
{
x = 1;
}
else
{
//this won't be called ever
}
if(x != 1)
{
x = 1;
}
if( x == 1)
{
//this WILL be called ever
}
( i meant to say , this WILL be called , "ever" is a mistype in the second case )
 
yeah but i don't have "x = 1; " in my code
 
you are right , give me a second to check
 
3:39 AM
what i have is
if(x<10){
x++;
}else{
//WILL BE CALLED
}
 
yes but it will be called next frame
for 1 frame it will be drawn in the "wrong" place
you need if/if not if/else
try it
if (chronoR > 0) {
chronoR -= speed * Time.deltaTime;
transform.Rotate (0, 0, -speed * Time.deltaTime, Space.World);
myEul.z += speed * Time.deltaTime;
} if (chronoR <= 0) {
transform.rotation = Quaternion.Euler (targetEul);
//transform.rotation = Quaternion.Slerp (transform.rotation, targetQuat, 100);
canRotate = true;
doRight = false;
}
 
hmm
 
you have a logic error
:)
 
nope :3
and here is why
suppose what you said it's true
and that my else is called in the next frame
or even called twice
this :
transform.rotation = Quaternion.Euler (targetEul);
the targetEul variable
is always the same
it only get updated when i press the arrow keys
 
i know
 
3:44 AM
(btw i just wrote your code, same result)
 
you mean that
4th frame of the 2nd gif is still doing the "weird" behavior ?
 
yup
 
the code i pasted was for the "right" arrow key only
 
i didn't copy past your code
 
there is no way the code i pasted you is weird on the 4th frame
 
3:45 AM
i just changed the else to if like you said
here is the entire project :)
it's just 100ko
 
i am not using unity
if yo changed the else
 
okay
 
to if ( randomvariable <= 0 )
it will never be drawn like the 4th frame of the 2nd gif
if it is
then you are doing it wrong
 
do it for the left and right as well
to be sure, because i don't know what you are doing
if it still doesn't work
i think i know how you should do it
tell me your results
 
3:49 AM
i did it for both prntscr.com/6js7bg
 
it didnt work ?
 
and the print message is printing only once so your "theory" is correct
but i still have the bad effect
the flimming thing
 
ok
 
flipping*
 
do this
actually, first answer me this
is Time.DeltaTime in unity in seconds on milliseconds ?
 
3:50 AM
milliseconds
 
are you sure about that ?
 
sorry no no
seconds
 
yea, i remembed seconds as well
try if (chronoR <= 0.1f)
this will work
 
...
 
but will be "choppy"
 
3:52 AM
what difference will that make ?
the problem is not there :p
 
this is why i told you to use if (chronoR <= 1/60 + 0.01f)
it is
 
no its not
the problem is here :
 
believe me
it is
 
targetEul = transform.eulerAngles;
targetEul.x += 90;
 
what you are doing
is like saying
 
3:52 AM
and then
transform.rotation = Quaternion.Euler (targetEul);
 
i want to have a value from 0-100
so i will set it to 100
 
there is some weird conversion happend
okay you know what
forget about animating the object
say we don't have time
 
and then i will do if (value < 0) { value -= 10; } , this will make the variable go as low as -10 !
 
okay ?
sorry mate
but you're seeing the problem from a different angle
 
do this for me , "if (chronoR <= 0.1f)" and check that it works for me
instead of if (chronoR <= 0)
 
3:55 AM
no it's useless
because
if i do this :
 
check it please
 
if (Input.GetKeyDown (KeyCode.LeftArrow)) {

targetEul = transform.eulerAngles;
targetEul.x += 90;
transform.rotation = Quaternion.Euler (targetEul);

}
in that one there is NO TIME INVLVED
no counters
no animation
you press a button and that's it, it rotate
it still give me the "weird flipping"
 
is it because you have a starting rotation ?
 
what do you mean starting rotation ?
you mean the cube is already rotated since the start ?
(because it looks "perspective" ?)
 
yes
 
3:59 AM
i thought about that too
the cube start rotation is 0 0 0
i rotated the camera instead
see this :
if (Input.GetKeyDown (KeyCode.LeftArrow)) {
targetEul = transform.eulerAngles;
targetEul.x += 90;
transform.rotation = Quaternion.Euler (targetEul);
}
if (Input.GetKeyDown (KeyCode.RightArrow)) {
targetEul = transform.eulerAngles;
targetEul.z += 90;
transform.rotation = Quaternion.Euler (targetEul);
}
 
this makes it stutter ?
 
"stutter" ?
it still do the weird flipping yes
 
hmm
let me think
i downloaded your project
and i am trying it now
 
cool thanks
 
i don't see any flipping at all
 
4:09 AM
what did you changed ?
 
nothing, i just removed the whitespaces and reformatted it cause its driving me mad :)
 
lol
it it still happening
 
what keys do you press exactly ?
 
alternate between left and right
randomely
 
left right left right left right left right
or not necessarily
?
 
4:11 AM
yes
but it doesn't have to be in order
but you'll see the flipping
am sure
so ?
 
trying to see what exactly the problem is
what exactly is the use of meEul ?
myEul *
 
nothing i just wanted to see the real objet euler angle in the inspector
you can ignore it
ps:
it's 5am here, i really need to sleep,
if you found a solution please post it in the main thread

Thanks for the effort man, i really appreciate it :)
good night
 
6:20 here , good night :)
 
haha
happy coding, batman
 
you too
 
4:21 AM
(y)
 
 
1 hour later…
5:36 AM
check how the code looks like now
not even 1 page long
 

last day (14 days later) »