What is the most efficient way of setting a rotation of a single axis of a GameObject without changing the other values?
what jaap did can be done by transform.rotate and i think it's not what you want and you want to set the value directly. you can use his code but remove the "rot*" and then the code will be
transform.rotation = Quaternion.Euler(0, 90, 0);
if you want to add some value to any of the x,y,z values simply use
transform.Rotate (0,0,10);
this will add 10 to Z value
I think this is what you're looking for :
// Sets the transforms rotation to rotate 30 degrees around the y-axis
transform.rotation = Quaternion.AngleAxis(30, Vector3.up);
Don't know what you exactly want but maybe this is it:
var rot = transform.rotation;
transform.rotation = rot * Quaternion.Euler(0, 90, 0); // this is 90 degrees around y axis
Try this
transform.rotation = Quaternion.Euler(transform.eulerAngle.x, 30, transform.eulerAngle.z);
The rotations are stored as quaternions, so you can store the appropriate quaternion and set transform.rotation to that.
Perhaps an explanation of what you are trying to accomplish would help. If you go into "Debug mode" and watch the rotation values change, you can see how just changing one axis's value isn't really more efficient. It only looks that way in "Normal mode".
// To set Axis.Z to “0”
Vector3 eulerRotation = transform.rotation.eulerAngles;
transform.rotation = Quaternion.Euler(eulerRotation.x, eulerRotation.y, 0);,// to set Axis.Z to 0
Vector3 eulerRotation = transform.rotation.eulerAngles;
transform.rotation = Quaternion.Euler(eulerRotation.x, eulerRotation.y, 0);
You can try:
transform.eulerAngle.x/y/z = NUMBER;
It has to be multiplied by the original rotation if you do it this way.
Quaternion originalRot = transform.rotation;
transform.rotation = originalRot * Quaternion.AngleAxis(degrees, Vector3.Up);
I hate how this works, but it works…
Vector3 currentRotation = Vector3.zero; //Same format as inspector
void Update () {
this.transform.rotation = Quaternion.identity;
this.transform.Rotate(currentRotation);
}
// To set Axis.Z to “0”
Vector3 eulerRotation = transform.rotation.eulerAngles;
transform.rotation = Quaternion.Euler(eulerRotation.x, eulerRotation.y, 0);
You could also force it by using this:
this.transform.eulerAngles = new Vector3(0f, transform.eulerAngles.y, 0f);
Just be sure that you are not already forcing chosen rotation (axis) on this body, otherwise it will act strangely, such as rotating on chosen rotation (axis) until the function is stopped.
For future generations:
transform.rotation = Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, 0);
This only sets z and leaves the rest unchanged.