- Home /
Smooth 90 degrees rotation on input . (infinite number of times)
Hi guys,
I have this game object that I am trying to rotate 90 degrees clockwise or anticlockwise according to user input.
Every time the user presses a key the object should slowly rotate 90 degrees and wait there . if the user presses the same key again it will rotate 90 degrees farther in the same direction, otherwise if the player presses the negative key for , the object rotates 90 degrees in the opposite direction... and so on.
So far i have this script. but the rotation is faulty, since every-time the player does an input, the rotation values get reset to (0,0,0)
buttonz.x is -1 on keypress 'q' and 1 on keypress 'e'.
function Start () {
}
function Update () {
if (buttonz.x==-1) {
MyRotate();
buttonz.x=0;
}
if (buttonz.x==1) {
MyRotato();
buttonz.x=0;
}
}
function MyRotate() {
for(var i = 0; i<90; i+=2) {
//print ("Entered for. Value is " + transform.eulerAngles.z);
yield;
transform.eulerAngles = Vector3(0, 0, i);
}
}
function MyRotato() {
for(var i = 0; i>-90; i-=2) {
//print ("Entered for. Value is " + transform.eulerAngles.z);
yield;
transform.eulerAngles = Vector3(0, 0, i);
}
}
Please help...
Thank You
I'm trying to get rotations as well. I can't really give you an answer but all my research is pointing me towards quaternion.Slerp Looking up quaternion and slerp functions may point you in the right direction :) Wish I could help more.
Answer by robertbu · May 23, 2013 at 11:32 PM
You are directly assigning to 'eulerAngles' so that is giving your object an absolute rotation. So you code rotates the angle between 0 and 90 or 0 and -90. As a quick fix, you can change the code to use Transform.Rotate():
#pragma strict
function Update () {
if (Input.GetKeyDown(KeyCode.A)) {
MyRotate();
}
if (Input.GetKeyDown(KeyCode.S)) {
MyRotato();
}
}
function MyRotate() {
for(var i = 0; i<90; i+=2) {
//print ("Entered for. Value is " + transform.eulerAngles.z);
yield;
transform.Rotate(0,0,2);
}
}
function MyRotato() {
for(var i = 0; i>-90; i-=2) {
//print ("Entered for. Value is " + transform.eulerAngles.z);
yield;
transform.Rotate(0, 0, -2);
}
}
There is one problem with this code...it is not frame rate independent. That is, it rotates two degrees for each frame, so it will be twice as fast when Unity is run at 60 fps on the desktop compared to 30 fps it might run on a smart phone.
So here is a bit different take on it that is frame rate independent:
#pragma strict
public var speed = 55.0;
private var rotation = 0.0;
private var qTo = Quaternion.identity;
function Update () {
if (Input.GetKeyDown(KeyCode.A)) {
rotation += 90.0;
qTo = Quaternion.Euler(0.0, 0.0, rotation);
}
if (Input.GetKeyDown(KeyCode.S)) {
rotation -= 90;
qTo = Quaternion.Euler(0.0, 0.0, rotation);
}
transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, speed * Time.deltaTime);
}
The y axis value is the 2nd float value of a Vector3. i.e. Vector3(x,y,z) or in this case Quaternion.Euler(x,y,z), so in this case to change it to y axis change lines 12 and 17 to...how do you change it to a y axis
qto = Quaternion.Euler(0f,rotation, 0f);
put this into c# as i work in c# also the quateron.identity doesnt work on c#
Well, yes, that is unityScript. But Quaternion.identity works in C# just fine. FYI, unityScript stopped being the recommended language, in favor of C#, a few years ago.
But beyond that, this is just pointing you in the right direction. Transform.Rotate is an easy way to spin yourself. Quaternion.Euler is how you make an (x,y,z) angle. Quaternion.RotateTowards is a way to gradually spin one angle into another one.
If you look those up, you'll find the official inputs, C# examples, and lots more examples of people using them. Rotations take a while to learn, and you'll probably have to it learn a little, since you won't find exactly what you need prewritten.
Your answer
Follow this Question
Related Questions
Transform a Vector by a Quaternion 1 Answer
How do I align my player transform to waypoints and without restricting Z Axis rotation? 2 Answers
Instantiate GameObject towards player 0 Answers
Multiple Fire on different launcher GameObjects 1 Answer
Rotating a Vector direction by another Vector direction in one axis 1 Answer