- Home /
am I using degrees, radians, rotation or quaternion ?
I'm trying to rotate a rigidbody on the Y-axis using MoveRotation , Input "Horizontal" and time, but cannot see where this is breaking.
Steps :
make a target rotation , make it equal to this.transform.[current rotation in degrees]
increase or decrease target rotation's Y (in degrees) by Input "Horizontal" and deltaTime
'snap to target rotation - euler angles degrees' with rigidbody.MoveRotation(target rotation);
Script :
var targetRotAngle : Quaternion = Quaternion.Euler (0, 0, 0);
var turnSpeed : float = 10.0;
function FixedUpdate () {
targetRotAngle = Quaternion.Euler (0, transform.eulerAngles.y, 0);
targetRotAngle.y += Input.GetAxis("Horizontal") * Time.deltaTime * turnSpeed;
rigidbody.MoveRotation(targetRotAngle);
}
Answer by aldonaletto · Mar 22, 2012 at 02:24 PM
As @syclamoth said, the quaternion components x,y and z have nothing to do with the familiar Euler angles - don't mess with them directly!
I would use a trick similar to that used in Orient vehicle to ground normal: keep the desired angle about Y in a variable, and assign Quaternion.Euler(0, angle, 0) to transform.rotate - or pass it to rigidbody.MoveRotation():
var targetRotAngle: float = 0.0; var turnSpeed : float = 10.0;
function Update () {
targetRotAngle += Input.GetAxis("Horizontal") Time.deltaTime turnSpeed;
transform.rotation = Quaternion.Euler(0,targetRotAngle%360,0);
} I moved the code to Update and used transform.rotation because Input is synced to the update cycle - reading it in FixedUpdate may cause double or triple rotation increments. If you really need to use rigidbody.MoveRotation, split the Input reading and MoveRotation:
var targetRotAngle: float = 0.0; var turnSpeed : float = 10.0;
function Update () { // read the input in Update and apply to targetRotAngle: targetRotAngle += Input.GetAxis("Horizontal") Time.deltaTime turnSpeed;
}
function FixedUpdate(){ // rotate the rigidbody in FixedUpdate: rigidbody.MoveRotation(Quaternion.Euler(0,targetRotAngle%360,0));
} NOTE: Pass the angle in modulo 360, since angles out of range may produce weird results.
Thanks for posting back. And reading over to this question . And showing a method of storing my target angle in a var after being separately calculated, then applying it.
All my program$$anonymous$$g is self-taught from forums, I appreciate all the replies. With some previous very basic use of Flash (AS2), I am blown away by what I have made with Unity in 3 months.
O$$anonymous$$, all of my rotation questions are co$$anonymous$$g from one standard I picked up : transform is used on gameObjects ; force and torque is used on rigidbody's .
$$anonymous$$y basic understanding was : to get real physics effect (colliding with other objects) I had to use rigidbody's , and manipulate them with force and torque in a FixedUpdate loop. Also using addForce And transform was wrong, and could lead to strange consequences with the physics engine.
SO the root question I have finally distilled to is : how to I correctly manipulate a rigidbody's angle of an axis to a specific angle over input and time ?
Saying that, I guess I should be applying torque, and when target angle is reached apply opposite torque. But on another project (pinball), it was suggested to me that Slerp was better than torque , with all the additional issues of dampening or slowing to target angle.
After 2 days of thinking about this problem, I have also discovered another command : Quaternion.FromToRotation .
Or should I never have gone down this thought path and just Slerp my rigidbody's ? (or would that re-create my gimble lock problem...) Sorry guys, but I feel like I'm stuck in a perpetual loop with one silly little problem over 3 posted questions . Again, all the time given by yourselves and others on this forum have greatly helped me, so the replies are much appreciated.
Rigidbodies are hard to position precisely: they are good for things like obstacles, projectiles and other things that will be thrown, hit, launched or fall to the ground. I would use a CharacterController ins$$anonymous$$d: you can move and rotate it more precisely with $$anonymous$$ove (see example in http://unity3d.com/support/documentation/ScriptReference/CharacterController.$$anonymous$$ove.html). The question I've mentioned about following the terrain normal used a CharacterController to control a vehicle's behaviour.
Answer by syclamoth · Mar 22, 2012 at 02:01 PM
Pretty much, 'targetRotAngle' is a Quaternion, not a 3-axis rotation. Quaternions are to complex numbers what complex numbers are to real numbers, but don't let that worry you- all you really need to know is that anything you can't do with the inbuilt Quaternion library, you should ask a mathematician about. Modifying the values directly- right out. Don't do it unless you really understand what quaternions are.
Just because it's created with the '.Euler' factory, doesn't mean it's a euler angle on the inside.
Thanks for posting back. All my program$$anonymous$$g is self-taught from forums, I appreciate all the replies. With some previous very basic use of Flash (AS2), I am blown away by what I have made with Unity in 3 months.
O$$anonymous$$, all of my rotation questions are co$$anonymous$$g from one standard I picked up : transform is used on gameObjects ; force and torque is used on rigidbody's .
$$anonymous$$y basic understanding was : to get real physics effect (colliding with other objects) I had to use rigidbody's , and manipulate them with force and torque in a FixedUpdate loop. Also using addForce And transform was wrong, and could lead to strange consequences with the physics engine.
SO the root question I have finally distilled to is : how to I correctly manipulate a rigidbody's angle of an axis to a specific angle over input and time ?
Saying that, I guess I should be applying torque, and when target angle is reached apply opposite torque. But on another project (pinball), it was suggested to me that Slerp was better than torque , with all the additional issues of dampening or slowing to target angle.
After 2 days of thinking about this problem, I have also discovered another command : Quaternion.FromToRotation .
Or should I never have gone down this thought path and just Slerp my rigidbody's ? (or would that re-create my gimble lock problem...) Sorry guys, but I feel like I'm stuck in a perpetual loop with one silly little problem over 3 posted questions . Again, all the time given by yourselves and others on this forum have greatly helped me, so the replies are much appreciated.