You can set rotation on the local axis in Unity using the editor interface or through scripting.
Using the Unity Editor:
- Select the GameObject: In the Hierarchy window, select the GameObject you want to rotate.
- Toggle to Local Rotation Mode: In the Scene view toolbar, locate the "Global/Local" toggle. Click it to switch to "Local" rotation mode. This displays the rotation gizmo relative to the object's own axes, rather than the world axes.
- Rotate the GameObject: Use the rotation gizmo to rotate the GameObject around its local X, Y, or Z axes.
Using C# Scripting:
You can directly manipulate the transform.localRotation
property in your scripts.
using UnityEngine;
public class RotateObject : MonoBehaviour
{
public Vector3 rotationAmount; // Rotation in degrees per second
void Update()
{
// Option 1: Add to the existing local rotation
transform.Rotate(rotationAmount * Time.deltaTime, Space.Self);
// Option 2: Directly set the local rotation using Euler angles
// transform.localRotation = Quaternion.Euler(rotationAmount.x * Time.deltaTime, rotationAmount.y * Time.deltaTime, rotationAmount.z * Time.deltaTime);
// Option 3: Increment rotation then apply
// Quaternion newRotation = Quaternion.Euler(rotationAmount * Time.deltaTime);
// transform.localRotation *= newRotation;
}
}
Explanation of the Scripting Methods:
-
*`transform.Rotate(rotationAmount Time.deltaTime, Space.Self)
**: This is the simplest way. The
Space.Selfargument ensures the rotation is applied relative to the GameObject's local axes.
rotationAmountshould be a
Vector3representing the degrees of rotation around the X, Y, and Z axes per second.
Time.deltaTime` is important to ensure the rotation is framerate-independent. -
transform.localRotation = Quaternion.Euler(...)
: This method directly sets thelocalRotation
property.Quaternion.Euler()
creates a quaternion from Euler angles (X, Y, Z rotations in degrees). Directly settinglocalRotation
replaces the existing rotation. Be careful using this method, as it can lead to unexpected behavior if you're not careful about how you accumulate rotations. -
*`transform.localRotation = newRotation;`**: This multiplies the current local rotation with a newly created rotation based on Euler angles. This method is useful for accumulating rotations over time, ensuring they are applied relative to the GameObject's local coordinate system. This is particularly useful when you want to rotate around multiple axes.
Important Considerations:
-
Quaternions vs. Euler Angles:
transform.localRotation
is aQuaternion
. While you often work with Euler angles (degrees) for defining rotations, Unity internally uses Quaternions to represent rotations because they avoid issues like gimbal lock. -
Order of Rotation: When using Euler angles, the order in which the rotations are applied (X, Y, Z) matters.
-
Combining Rotations: If you need to combine rotations, it's generally best to work with Quaternions directly. Use
Quaternion.AngleAxis()
to create a rotation around a specific axis and then multiply Quaternions to combine them.
By understanding these methods, you can effectively control the rotation of GameObjects along their local axes in Unity, either through the editor or with scripts.