Here's a Unity 6-compatible C# example that:
- Displays a UI button labeled "Get Rotation Vector".
- When clicked:
- Generates a random rotation vector.
- Rotates an object along that direction.
🎮 Steps:
- Create a 3D object in your scene (e.g., a Cube).
- Attach the script below to a new empty GameObject (e.g., "RotationManager").
- Assign the 3D object in the inspector to the public field.
- Add a UI Button, and hook up its OnClick() to call GetRotationVector().
✅ RotationController.cs
using UnityEngine;
using UnityEngine.UI;
public class RotationController : MonoBehaviour
{
public GameObject objectToRotate; // Assign in inspector
public Text vectorDisplayText; // Optional: Assign a UI Text to display the vector
private Vector3 currentRotationVector;
void Start()
{
currentRotationVector = Vector3.zero;
}
public void GetRotationVector()
{
// Generate a random unit vector
currentRotationVector = Random.onUnitSphere;
// Optionally show it in UI
if (vectorDisplayText != null)
vectorDisplayText.text = $"Rotation Vector: {currentRotationVector.ToString("F2")}";
// Start rotating along the new vector
StopAllCoroutines();
StartCoroutine(RotateAlongVector(currentRotationVector));
}
System.Collections.IEnumerator RotateAlongVector(Vector3 direction)
{
float rotationSpeed = 90f; // degrees per second
while (true)
{
objectToRotate.transform.Rotate(direction, rotationSpeed * Time.deltaTime, Space.World);
yield return null;
}
}
}
🧩 Unity UI Setup
- Create a UI → Canvas and add:
- Button → set its OnClick() to call RotationController.GetRotationVector()
- (Optional) Text → assign to vectorDisplayText to show the vector
- Create a Cube or any 3D object, and assign it to objectToRotate.
Let me know if you want it to rotate for a fixed time instead of continuously, or support other features like rotation speed sliders!
🌀 Unity 6: Rotate a 3D Object Along a Random Vector and Reset After a Timer Using UI
Rotating objects dynamically in Unity is a powerful way to add interactivity and visual appeal to your game or simulation. In this guide, you’ll learn how to build a simple UI-based system where a button click triggers a 3D object to rotate along a randomly generated vector — and automatically return to its original orientation after a few seconds.
✅ What You’ll Build
-
A UI button labeled "Get Rotation Vector"
-
A 3D object (like a cube) that rotates along a randomly generated vector when the button is clicked
-
A timer mechanism that smoothly returns the object to its original rotation after a fixed period
🧱 Step-by-Step Guide
1. 🎮 Setup Your Unity Scene
-
Open Unity 6 and create a new 3D project.
-
In the Hierarchy:
-
Right-click → 3D Object → Cube → name it
RotatingObject
-
Right-click → UI → Canvas
-
Inside Canvas, create a Button
-
Create a Text (UI) element if you want to show the vector.
-
-
-
Right-click in Hierarchy → Create Empty → name it
RotationManager
2. 🧠 Attach This Script to RotationManager
Create a C# script called RotationController.cs
and attach it to the RotationManager
GameObject.
3. 🧩 UI Button Setup
-
Select your Button in the Canvas.
-
In the OnClick() list:
-
Drag the
RotationManager
GameObject into the slot. -
Select
RotationController → GetRotationVector()
.
-
4. 🔗 Hook Up References in Inspector
-
Drag the Cube (or whatever 3D object) into
Object To Rotate
. -
If you’re using a UI Text, assign it to
Vector Display Text
.
🎨 Bonus: UI Design Tips
-
Use
TextMeshPro
for cleaner UI fonts. -
Add a rotation indicator arrow for visual direction feedback.
-
You could replace
Random.onUnitSphere
with user-defined axes or sliders for more control.
✅ Result
Now, when you run the game and click the "Get Rotation Vector" button:
-
A random rotation vector is generated.
-
The cube rotates in that direction for
3 seconds
. -
It then smoothly interpolates back to its original rotation.