- Home /
Need help converting js to C# - rigidbody.rotation.z
Hi!
Need help to convert js line to C#:
rigidbody.rotation.z = 0;
alert ... this is totally and completely wrong. (heh!)
you can not modify the xyzw paramaters of a quaternion
it says so very clearly on the unity doco page.
it is very - very - confusing that unity used the letters "xyz" because the four paramaters of a quaterion have utterly no relationship at all to the three dimensions of space.
it is also simply a mistake that Unity allows them to be modified or looked at. there is absolutely no reason, ever, that you would look at them (far less set them). it's exactly like say, there'd be utterly no reason to look at the internals of PhysX as it's working, or whatever.
If you have advanced mathematical skills, you can read in 1000s of pages on the internet explanations of how quaternions are constructed, but it is irrelevant here. All you need to know to be a gem engineer is that they are a "thing" which can represent rotations, avoiding gimbal lock, and which for a good mathematical Group.
@ineteye, you can not do what you are trying to do.
(the fact that in c# you can't modify individual paras is irrelevant. TBC you cannot do that in javascript.)
note guys ..
this http://unitygems.com would be incredibly helpful to you, note the quaternions article
Answer by Random username · Oct 13, 2012 at 06:37 PM
Hey, the problem is that you can't modify a single axis directly. Try doing this instead:
Vector3 r = rigidbody.rotation.eulerAngles;
rigidbody.rotation = Quaternion.Euler(new Vector3(r.x,r.y,0));
The reason I'm converting to euler angles is simply because Quartonians are dangerous to modify lol.
Quaternions aren't dangerous to modify, they just don't work the same way, and can therefore behave in an unexpected way. $$anonymous$$odifying a quaternion like you would with Euler angles might rotate the GameObject in the wrong direction.
Or would you call that dangerous?
referring to the internal four properties of a quaterion (which are badly named xyzw by Unity), the word "dangerous" is not really correct.
you can not achieve anything by modifying or reading the four internal values of a quaternion.
just as Random explains, you set or create a quaternion using the euler angles feature (or by building it from other quaternions, or handy functions like LookAt, etc etc)
Sorry, dangerous was a dumb word to use. I didn't mean it would literally, just that it's probably not going to do what you expect.
right. it absolutely, definitely, will not do "what ineteye would expect" in his example!!!
your answer is perfect