- Home /
Rotation: rotate by a defined angle
Hi everyone,
I have been trying to get a smooth and visually pleasing way of have an object (plane in my case) tilt by about 50 degrees on a command.
So far I have tried:
Case 1:eulerAngle rotations --> Works pretty well but way to jerky (happening in one frame) if i added the Time.deltaTime it only did what the amount of degrees for 1 second.
Case 2:Quaternions --> Never got this to work well... continued to rotate when ever the button was pressed (could not get it to stop at 50 degrees).
Case 3: calling another function --> this by far works the best, however, if the key is lifted "GetButtonUp" before the angle movement for "GetButtonDown" is finished (ie: it only had time to go 20 degrees) the plane overshoots its level position.
In theory I feel like this should be really simple! heres a picture to make it clear (i eventually want to connect it to my GUI joystick, but for now I'm happy to get it working on a button down for keyboard)
for anyone looking for a similar solution:
--Heres the NEW code-- that I am using based on the answer suggestion for my situation
(there are still some issues with how I return to the flat, I will post the updated script when I work it out. Thanks for the help!**
var currentx = transform.eulerAngles.x; var currenty = transform.eulerAngles.y;
if(Input.GetAxis("a")){
fromRotation = transform.rotation; toRotation = Quaternion.Euler(currentx,currenty,110); transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime * lerpSpeed);
}else{isTurning = 1;}
if(Input.GetAxis("d")){
fromRotation = transform.rotation; toRotation = Quaternion.Euler(currentx,currenty,-110); transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime * lerpSpeed);
}else{isTurning = 1;}
//isTurning 0=yes 1=no (so flatten out) if(isTurning == 1){ fromRotation = transform.rotation; toRotation = Quaternion.Euler(currentx,currenty,0); transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime * lerpSpeed); isTurning = 0; }
Thanks for your help
Answer by Myth · Feb 02, 2011 at 11:30 PM
a little bitta code taken straight outta something I'm putting together that may be able to help
fromRotation = transform.rotation; toRotation = Quaternion.Euler(aa,bb,cc); transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime * lerpSpeed);
the " Lerp " part being the important bit
YES!! Thank you! this seems to work perfectly, its pretty much exactly what I was looking for.
Your answer
Follow this Question
Related Questions
Problem with "if or" statement. 1 Answer
How can I rotate an object with certain degrees? 2 Answers
Rotate object in 45 degree steps 1 Answer
What is the most efficient way to rotate an object? 3 Answers
90 degrees isnt? 2 Answers