🌀 Unity 6: Rotate a 3D Object Along a Random Direction Vector Using a UI Button

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

  1. Create a UI → Canvas and add:
    • Button → set its OnClick() to call RotationController.GetRotationVector()
    • (Optional) Text → assign to vectorDisplayText to show the vector
  2. 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

  1. Open Unity 6 and create a new 3D project.

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

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

csharp
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class RotationController : MonoBehaviour
{
public GameObject objectToRotate;
public Text vectorDisplayText;
public float rotationDuration = 3f;
public float rotationSpeed = 90f;

private Vector3 rotationVector;
private Quaternion originalRotation;
private Coroutine rotateCoroutine;

void Start()
{
if (objectToRotate != null)
{
originalRotation = objectToRotate.transform.rotation;
}
}

public void GetRotationVector()
{
if (objectToRotate == null) return;

// Store original rotation
originalRotation = objectToRotate.transform.rotation;

// Generate a random rotation vector (unit vector)
rotationVector = Random.onUnitSphere;

if (vectorDisplayText != null)
vectorDisplayText.text = $"Rotation Vector: {rotationVector.ToString("F2")}";

// If already rotating, stop it first
if (rotateCoroutine != null)
StopCoroutine(rotateCoroutine);

// Start new rotation
rotateCoroutine = StartCoroutine(RotateTemporarily(rotationVector));
}

IEnumerator RotateTemporarily(Vector3 direction)
{
float timer = 0f;

// Rotate for a fixed duration
while (timer < rotationDuration)
{
objectToRotate.transform.Rotate(direction, rotationSpeed * Time.deltaTime, Space.World);
timer += Time.deltaTime;
yield return null;
}

// Smoothly return to original rotation
yield return StartCoroutine(ReturnToOriginalRotation());
}

IEnumerator ReturnToOriginalRotation()
{
Quaternion startRotation = objectToRotate.transform.rotation;
float time = 0f;
float duration = 1.5f;

while (time < duration)
{
objectToRotate.transform.rotation = Quaternion.Slerp(startRotation, originalRotation, time / duration);
time += Time.deltaTime;
yield return null;
}

objectToRotate.transform.rotation = originalRotation;
}
}


3. 🧩 UI Button Setup

  1. Select your Button in the Canvas.

  2. 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:

  1. A random rotation vector is generated.

  2. The cube rotates in that direction for 3 seconds.

  3. It then smoothly interpolates back to its original rotation.

Leave a Reply

Your email address will not be published. Required fields are marked *