- Home /
Quaterion is rotating my character improperly?
When I hit trigger. Trigger is to rotate my character 90 degrees on the y axis. But for some reason it's rotating my character 180 degrees. I double checked there is no duplicate of the trigger.. and the scripts only on the trigger.... Ive tried setting the Quaterion to 45 just to see if it made a difference, but it doesn't, so I need advice.
Heres my script
private var turn = false;
function OnTriggerEnter (other:Collider){
if(other.CompareTag("SSPlayer")){
turn = true;
}
}
function LateUpdate(){
var player = GameObject.FindGameObjectWithTag("SSPlayer");
if(turn == true){
player.transform.rotation = Quaternion(0,45,0,0);
var cam = GameObject.FindGameObjectWithTag("CamToggle");
var cs = cam.GetComponent(CameraToggle);
cs.CamSelect = 1;
var ssp = GameObject.FindGameObjectWithTag("SSPlayer");
var sp1 = ssp.GetComponent(SSController);
sp1.cont1 = 1;
var sp2 = ssp.GetComponent(SSController2);
sp2.cont2 = 1;
turn = false;
}
}
Thanks in advance to anyone who answers!
Quaiternions are utterly unlike what you think.
It says extremely clearly in large letters here:
"never access or modify individual Quaternion components"
http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.html
Qiote simply, never access or modify individual Quaternion components.
Answer by Matt-Downey · Aug 25, 2012 at 10:40 AM
Quaternions are not what you think they are. Quaternions are an odd 4D space that will never faulter when doing rotations, but as a result it has absolutely nothing to do with Eulers Angles (which you are currently using). I lied a little in that there are links between the two, but you will have to learn the math from wikipedia or khan academy if you really want to use it.
What you want is the rotate method in the transform class.
transform.Rotate(0,90,0,Space.Self);
each class has tons of useful methods and variables in the API.
For instance: transform has Rotate, RotateAround, Translate, ViewportToRay, etc all of which you can see here.
Since I believe the background math is often more useful for understanding, the reason (0,90,0,0) will rotate the player 180 degrees lies in two major factors.
Firstly the squares of all factors (x,y,z,w) must add up to 1, so (0,90,0,0) is actually normalized to (0,1,0,0) before any calculations are made. [edit2: this is why (0,90,0,0) and (0,45,0,0) do the same thing, because they both become (0,1,0,0) due to normalization]
What happens next I'm a little shifty on, but unless you are multiplying by (0,0,0,1) [edit: or (0,0,0,-1)] the rotation will always change.
In this case (1,0,0,0); (0,1,0,0); & (0,0,1,0) might all rotate the player 180 degrees (just moving around different axes. The first case rotates the player around the x axis 180 degrees, second y, third z.
The larger the 4th value (dubbed w), the less the quaternion will change. Also here's Wikipedia Quaternion to Euler Page:
http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
The w, which is at the end in unity is at the beginning in almost all technical papers I've seen [it looks like this(w,x,y,z) normally]. Notice how (w,x,y,z) [convention] is alphabetical compared to Unity's (x,y,z,w).
I just changed it to player.transform.rotation = Quaternion.Euler(0,90,0);
Though, it rotated to 90.0001... That is a bit strange is it not?
That's a rounding error, you can ignore it. It has to do with a problem called drifting (it is what it sounds like).
Floating point numbers are not 100% accurate, luckily though most of the decimal places are entirely unnoticable (floats count 6-7 digits places). For instance, for the mouse cursor to drift an extra degree (which in itself is hard to notice), it would probably take 10,000 frames, which at 60 frames a second won't happen very fast.
Banks handle data differently because they know if money starts drifting upwards, the pennies will start to add up, so they never use floating point numbers as far as i know but ins$$anonymous$$d "long"-type numbers or large or big (or something like that, I forget).
This article could be down your alley if you want to learn about drifting (you also learn methods of fixing it, but it's more for the knowledge than in the attempt to fix the problem, since drifting won't break your game (aside from animations in very long games as mentioned in the article)): http://gamasutra.com/view/news/173606/Indepth_$$anonymous$$atrices_rotation_scale_and_drifting.php