- Home /
Spaceship Rotation conversion
I'd like to calculate a rotation to be able to control a spaceship.
first of all, I would get the current rotation desired since last update.
var presentRotation=Quaternion.identity
presentRotation.eulerAngles=Vector3(x,y,z) (x,y,z being decided by the inputs)
then I would probably get the total desired rotation with the previous total rotation:
rotation*=presentRotation
Now I would get the spaceship transform rotation and transfer it to quaternions. My problem comes after that, when I would like to get the rotation between the two rotation, get the euler angles of that rotation and then apply some torque according to the rotation needed. I can't see how I can obtain those angles. Is there a way to do that or is there a better way around?
Thanks
I could probably only apply torque to my inputs and automatically applying torque against the angular speed, so it would kinda replicate angular drag. thing is, I want to stop my angular motion automatically so it's easier to pilot. I want to fake a automatic system so it doesn't look like I only put angular drag.
Answer by aldonaletto · Jun 03, 2012 at 11:56 PM
If you want to rotate the ship with your controls, the easiest way is to use Rotate:
function Update(){ var yaw = Input.GetAxis("Horizontal") * Time.deltaTime * 60; // yaw at 60 deg/sec var pitch = Input.GetAxis("Vertical) * Time.deltaTime * 45; // pitch at 45 deg/sec transform.Rotate(pitch, yaw, 0); }But if you want to set the ship rotation to some specific XYZ angles, you can save the initial eulerAngles at Start, "rotate" this angles mathematically and assign them to transform.eulerAngles:
var angles: Vector3;
function Start(){ angles = transform.eulerAngles; }
function Update(){ var yaw = Input.GetAxis("Horizontal") Time.deltaTime 60; // yaw at 60 deg/sec var pitch = Input.GetAxis("Vertical) Time.deltaTime 45; // pitch at 45 deg/sec angles.y = (angles.y + yaw) % 360; angles.x = (angles.x + pitch) % 360; transform.eulerAngles = angles; } The last alternative is to have the axes assigned to different children:
ShipYaw // use this object to move and yaw
ShipPitch // use this only to pitch
It's like above, but you should set ShipYaw.eulerAngles to (0, yaw, 0), and ShipPitch.localEulerAngles to (pitch, 0, 0).
Thing is, I'd like to use AddTorque for my physic to be as realistic as possible
You can apply torque ins$$anonymous$$d of setting the eulerAngles, like this:
function FixeedUpdate(){ var yaw = Input.GetAxis("Horizontal") * 5; var pitch = Input.GetAxis("Vertical) * 5; rigidbody.AddTorque(pitch, yaw, 0); }But be aware that you will not have a reliable indication of the current angles about XYZ: the property transform.eulerAngles is actually the property transform.rotation converted to/from Euler angles, and may return weird (although correct) results at some sectors. Euler angles is a redundant system: there are several XYZ combinations that result in exactly the same actual rotation. When you read transform.eulerAngles, Unity convert the quaternion transform.rotation to one of these combinations, but at certain regions other different combinations may be returned - one angle may suddenly jump from near zero to -90, while other angles jump to other weird values.
If you want good physics simulation, apply the torque; if you need precise positioning, forget about torque and set the euler angles directly - unfortunately, you can't have both at the same time.
Your answer
![](https://koobas.hobune.stream/wayback/20220613072320im_/https://answers.unity.com/themes/thub/images/avi.jpg)