Open EloiStree opened 4 weeks ago
🤖
// Rotate around the X axis
transform.Rotate(Vector3.right * rotationSpeed * Time.deltaTime);
// Rotate around the Y axis
transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
// Rotate around the Z axis
transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
// Rotate around the global X axis
transform.Rotate(Vector3.right * rotationSpeed * Time.deltaTime, Space.World);
// Rotate around the global Y axis
transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime, Space.World);
// Rotate around the global Z axis
transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime, Space.World);
// Set rotation to a specific Euler angle
transform.rotation = Quaternion.Euler(45, 90, 0);
// Set rotation to match another object's rotation
transform.rotation = targetObject.transform.rotation;
// Set rotation using Quaternion directly
transform.rotation = Quaternion.Euler(new Vector3(30, 60, 90));
// Smoothly rotate to target rotation using Lerp (Linear Interpolation)
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
// Smoothly rotate to target rotation using Slerp (Spherical Linear Interpolation)
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
// Rotate to look at a target position
transform.LookAt(targetPosition);
// Look at target with a specific upward direction
transform.LookAt(targetPosition, Vector3.up);
// Calculate direction to the target
Vector3 direction = targetPosition - transform.position;
// Calculate target rotation
Quaternion targetRotation = Quaternion.LookRotation(direction);
// Rotate towards the target rotation
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
// Rotate around a point (pivot) in world space
transform.RotateAround(pivotPoint, Vector3.up, rotationSpeed * Time.deltaTime);
// Get current rotation as Euler angles
Vector3 currentEulerAngles = transform.rotation.eulerAngles;
// Set rotation using Euler angles
transform.rotation = Quaternion.Euler(new Vector3(45, 90, 0));
// Rotate in the opposite direction
Quaternion inverseRotation = Quaternion.Inverse(targetRotation);
transform.rotation = inverseRotation;
Transform.RotateAround
// Rotate around a point in space
transform.RotateAround(pivotPoint, axis, angle);
Quaternion.AngleAxis
// Create a rotation around a specific axis
Quaternion rotation = Quaternion.AngleAxis(45, Vector3.up);
// Apply the rotation to the object
transform.rotation = rotation * transform.rotation;
Quaternion.FromToRotation
// Calculate the rotation needed to rotate from one direction to another
Quaternion rotation = Quaternion.FromToRotation(Vector3.forward, targetDirection);
// Apply the rotation
transform.rotation = rotation;
// Apply torque to rotate a Rigidbody
rigidbody.AddTorque(Vector3.up * torqueAmount);
// Set angular velocity directly
rigidbody.angularVelocity = new Vector3(0, rotationSpeed, 0);
This cheat sheet should cover most of the common rotation tasks you'll encounter in Unity3D.