Hey there!
This is my first post, pretty new to Unity not really sure how rotations work yet. I have an object that I want the player to be able to rotate in 90 degrees either way in all directions. I’ve made a function that takes in the x,y,z rotations as parameters (it’ll be 2 0’s and a 90 or -90 everytime it’s called). It works fine the first time you rotate or if you keep rotating over the same axis, but as soon as you say, rotate over the x and then try over the z it’s either the wrong direction or the wrong axis.
Here’s the code I have:
function Update () {
if(!moving){
//rotate
if (Input.GetKey(KeyCode.LeftArrow)) {
RotateAngle(0,0,90);
}
if (Input.GetKey(KeyCode.RightArrow)) {
RotateAngle(0,0,-90);
}
//flip
if (Input.GetKey(KeyCode.UpArrow)) {
RotateAngle(90,0,0);
}
if (Input.GetKey(KeyCode.DownArrow)) {
RotateAngle(-90,0,0);
}
if (Input.GetKey(KeyCode.A)) {
RotateAngle(0,90,0);
}
if (Input.GetKey(KeyCode.S)) {
RotateAngle(0,-90,0);
Debug.Log("Down");
}
}
function RotateAngle(angleFlip:int,angleSpin:int,angleRotate:int){
curEuler = transform.rotation.eulerAngles;
if (moving) return; // ignore calls to RotateAngle while rotating
moving = true; // set the flag
var newAngleFlip=curEuler.x+angleFlip;
var newAngleSpin=curEuler.y+angleSpin;
var newAngleRotate=curEuler.z+angleRotate;
while (Mathf.Abs(curEuler.y-newAngleSpin)>0||
Mathf.Abs(curEuler.x-newAngleFlip)>0||
Mathf.Abs(curEuler.z-newAngleRotate)>0){
curEuler.z = Mathf.MoveTowards(curEuler.z, newAngleRotate, rotSpeed*Time.deltaTime);
curEuler.x = Mathf.MoveTowards(curEuler.x, newAngleFlip, rotSpeed*Time.deltaTime);
curEuler.y = Mathf.MoveTowards(curEuler.y, newAngleSpin, rotSpeed*Time.deltaTime);
transform.eulerAngles = curEuler; // update the object's rotation...
yield; // and let Unity free till the next frame
}
moving = false;
}
Thanks in advanced!
Can you make use of the built in functions such as:
I usually refrain from using the built in Euler angles representations because of the low amount of control you you get over it, instead I keep my own Euler angle representation I can control myself entirely, and feed the direction back to Unity.
After reviewing your question a few times, sounds like you’re just rotating in world space rather than local space, so simply changing transform.rotation to transform.localRotation could solve your issues.
However, I’d rather use the Quaternions for this kind of rotation. Here’s how I’d implement the tween:
function RotateAngle(angleFlip:int,angleSpin:int,angleRotate:int){
if (moving) return; // ignore calls to RotateAngle while rotating
moving = true; // set the flag
var startRotation : Quaterion = transform.rotation; //Find the start rotation, use localRotation here if you want the rotations to be in local space instead, which sounds like it could be one of your issues.
var endRotation : Quaterion = startRotation * Quaternion.Euler(angleFlip, angleSpin, angleRotate); // calculate end rotation
var startTime : float = Time.time; // Get start time
var endTime : float = (Quaternion.Angle(startRotation, endRotation) / rotSpeed) + Time.time; // Calculate end time.
while (Time.time < endTime) {
var lerp : float = Mathf.InverseLerp(startTime, endTime, Time.time); // calculate lerp value
transform.rotation = Quaternion.Slerp(startRotation, endRotation, lerp); // Apply the rotation. Use localRotation here aswell if local space (see above)
yield;
}
transform.rotation = endRotation; // apply the fixed end rotation when done. Use localRotation here aswell if local space (see above)
moving = false;
}
Thanks for the replies… It still doesn’t work though :(. It works at the beginning… But then once you rotate it around a few times it starts doing the wrong direction for the key presses.
Just to be clear of the functionality I’m trying to get:
there is a camera aimed 1 side of a cube.
Pressing Up makes it rotate up 90 degrees.
Pressing Down makes it rotate down 90 degrees.
Same with Left and Right…
A and S make it twist 90 degrees (Example: the left side will become the top if you rotate to the right)
So pretty much you just can control this cube by rotating it 90 degrees in any direction.
Any other ideas on how to get this to work?
Got it to work using rotateAround
function RotateAngle(angleFlip:int,angleSpin:int,angleRotate:int){//degrees in each axis
if (moving) return; // ignore calls to RotateAngle while rotating
moving = true; // set the flag
//get end positions
var startRotation = transform.rotation;
var startPosition = transform.position;
transform.RotateAround(Vector3.zero, Vector3(angleFlip,angleSpin,angleRotate), 90);
var endRotation = transform.rotation;
var endPosition = transform.position;
transform.rotation = startRotation;
transform.position = startPosition;
var stopTime:float=Time.time+rotateTime;//want it to rotate for 1 second
while(Time.time<stopTime){
transform.RotateAround (Vector3.zero, Vector3(angleFlip,angleSpin,angleRotate), 90 * Time.deltaTime);
yield; // and let Unity free till the next frame
}
//jump to the right position because if you don't its a few degrees off..
transform.rotation = endRotation;
transform.position = endPosition;
moving = false;
}