EloiStree / HelloUnityKeywordForJunior

List of word nice to learn for Unity3D
0 stars 0 forks source link

Keyword: Rotation #148

Open EloiStree opened 4 weeks ago

EloiStree commented 4 weeks ago

image

EloiStree commented 4 weeks ago

🤖

1. Rotating Object Around Local Axes

// 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);

2. Rotating Object Around Global Axes

// 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);

3. Setting Rotation Directly

// 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));

4. Smooth Rotation (Lerp/Slerp)

// 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);

5. Look at a Target

// Rotate to look at a target position
transform.LookAt(targetPosition);

// Look at target with a specific upward direction
transform.LookAt(targetPosition, Vector3.up);

6. Rotating Towards a Direction

// 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);

7. Rotating Object Around a Point

// Rotate around a point (pivot) in world space
transform.RotateAround(pivotPoint, Vector3.up, rotationSpeed * Time.deltaTime);

8. Getting and Setting Euler Angles

// 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));

9. Inverse Rotation

// Rotate in the opposite direction
Quaternion inverseRotation = Quaternion.Inverse(targetRotation);
transform.rotation = inverseRotation;

10. Using Transform.RotateAround

// Rotate around a point in space
transform.RotateAround(pivotPoint, axis, angle);

11. Using 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;

12. Using 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;

13. Rotating with Physics (Rigidbody)

// 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.