How To Rotate Object In Unity 3D | Unity Tutorial
In this video tutorial, you’ll discover how to rotate objects in Unity 3D effortlessly. We’ll guide you through the basics of rotating objects using Unity’s Transform tools in the Editor, and then move on to scripting rotation with C# for more dynamic control. Learn how to apply continuous rotation with Transform.Rotate, implement user input for interactive experiences, and create smooth, fluid rotations.
WATCH FULL TUTORIAL ON YOUTUBE
=========================================================
Unity Projects - ON SALE!!!
1. Water Sort Puzzle 9000 Levels
2. Ball Sort Puzzle 9000 Levels
3. Sling Shot
=========================================================
Scripts:
Rotate.cs
using UnityEngine;
public class Rotate : MonoBehaviour
{
public float yRotationSpeed = 50f; // Speed of rotation around Y-axis
void Update()
{
// Calculate rotation amount based on speed and time
float yRotationAmount = yRotationSpeed * Time.deltaTime;
// Apply rotation around Y-axis
transform.Rotate(Vector3.up, yRotationAmount);
}
}
------------------------------------------------------------------------------------------------------------------------------------
DirectionalRotate.cs
using UnityEngine;
public class DirectionalRotate : MonoBehaviour
{
public float rotationSpeed = 100f; // Rotation speed in degrees per second
void Update()
{
// Initialize rotation angles
float rotateX = 0f;
float rotateY = 0f;
float rotateZ = 0f;
// Check for WASD input
if (Input.GetKey(KeyCode.W))
{
rotateX = rotationSpeed * Time.deltaTime; // Rotate around X-axis
}
if (Input.GetKey(KeyCode.S))
{
rotateX = -rotationSpeed * Time.deltaTime; // Rotate around X-axis
}
if (Input.GetKey(KeyCode.A))
{
rotateY = -rotationSpeed * Time.deltaTime; // Rotate around Y-axis
}
if (Input.GetKey(KeyCode.D))
{
rotateY = rotationSpeed * Time.deltaTime; // Rotate around Y-axis
}
if (Input.GetKey(KeyCode.Q))
{
rotateZ = -rotationSpeed * Time.deltaTime; // Rotate around Z-axis
}
if (Input.GetKey(KeyCode.E))
{
rotateZ = rotationSpeed * Time.deltaTime; // Rotate around Z-axis
}
// Apply rotation
transform.Rotate(Vector3.right, rotateX);
transform.Rotate(Vector3.up, rotateY);
transform.Rotate(Vector3.forward, rotateZ);
}
}
---------------------------------------------------------------------------------------------------------------------------------------
AdvancedDirectionalRotate.cs
using UnityEngine;
public class AdvancedDirectionalRotate : MonoBehaviour
{
public float xRotationSpeed = 100f; // Speed of rotation around X-axis
public float yRotationSpeed = 100f; // Speed of rotation around Y-axis
public float zRotationSpeed = 100f; // Speed of rotation around Z-axis
public bool startRotating = false; // Toggle for automatic rotation
public bool rotateXPositive = false; // Rotate positively around X-axis
public bool rotateYPositive = false; // Rotate positively around Y-axis
public bool rotateZPositive = false; // Rotate positively around Z-axis
private bool isRotating = false; // Current rotation state
void Start()
{
// Set initial rotation state based on startRotating flag
isRotating = startRotating;
}
void Update()
{
// Initialize rotation angles
float rotateX = 0f;
float rotateY = 0f;
float rotateZ = 0f;
// Check for WASD input to control rotation
if (Input.GetKey(KeyCode.W))
{
rotateX = xRotationSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S))
{
rotateX = -xRotationSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A))
{
rotateY = -yRotationSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.D))
{
rotateY = yRotationSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.Q))
{
rotateZ = -zRotationSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.E))
{
rotateZ = zRotationSpeed * Time.deltaTime;
}
// Check for space bar to toggle rotation
if (Input.GetKeyDown(KeyCode.Space))
{
isRotating = !isRotating;
}
// Apply rotation based on state
if (isRotating)
{
if (startRotating)
{
// Apply initial rotation based on flags
if (rotateXPositive)
{
transform.Rotate(Vector3.right, xRotationSpeed * Time.deltaTime);
}
if (rotateYPositive)
{
transform.Rotate(Vector3.up, yRotationSpeed * Time.deltaTime);
}
if (rotateZPositive)
{
transform.Rotate(Vector3.forward, zRotationSpeed * Time.deltaTime);
}
}
else
{
// Apply rotation based on input
transform.Rotate(Vector3.right, rotateX);
transform.Rotate(Vector3.up, rotateY);
transform.Rotate(Vector3.forward, rotateZ);
}
}
}
}