Unity Basics
  • Welcome
  • Getting Started with Unity Software
  • Helpful Shortcuts
  • The Unity Software Interface
    • The Default Interface
    • The Windows (Tabs)
      • Hierarchy Window
      • Scene Window
      • Project Window
      • Inspector Window
      • Game Window
      • Console Window
    • Other Layouts
      • Create a Custom Layout
  • Create
    • Creating Game Objects
      • 3D Primitives
      • Creating Game Objects in the Editor
      • Spawning Objects
      • Unhiding/Hiding Objects During Gameplay
      • Parenting
    • Creating Assets
    • Creating Components and Scripts
      • Adding Components in the Inspector
      • Creating Custom Components and Scripts
    • Creating Prefabs
  • Select and Update
    • General Selection
    • Search and Focus
    • Updating Game Objects
      • Updating Game Objects in the Editor
      • Updating Prefabs
    • Updating Assets
    • Updating Components
      • Update Components in the Inspector
      • Accessing Components Through Scripts
  • Delete
    • Deleting Game Objects
      • Deleting and Disabling Objects in the Editor
      • Using the Destroy() Function
    • Deleting Components and Scripts
      • Disabling and Removing Components in the Editor
      • Disabling and Removing Components During Gameplay
  • Translate, Rotate, and Scale
    • Intro to Transforms
    • Handy Transform Shortcuts
    • Translate
      • Positioning Game Objects in the Editor
      • Updating Position Through Script Code
      • Using the Transform.Translate() Function
    • Rotate
      • Rotating Game Objects in the Editor
      • Updating Rotation Through Script Code
      • Using the Transform.Rotate() Function
    • Scale
      • Resizing/Scaling Objects in the Editor
      • Updating Scale Through Script Code
    • Controlling Speed
    • Common Issues with Transforms
  • Materials
    • Material Basics
      • Creating and Applying Materials
      • Accessing Materials Through Code
  • Physics
    • Physics Basics
    • Colliders and Triggers
      • Collider Component Overview
      • Accessing Colliders Through Scripts
      • Common Issues: Colliders and Triggers
    • Rigidbody Component
      • Rigidbody Component Overview
      • Accessing the Rigidbody Component Through Scripts
      • Common Issues: Rigidbody Components
    • Common Issues: Physics and Velocity
  • Interaction
    • Interaction Basics
    • Keyboard Controls
    • Mouse Controls
  • Coding Basics
    • Intro to Scripts
    • Variables
    • Conditionals (If / Then / Else)
    • Arrays
    • Loops
    • Functions
Powered by GitBook
On this page
  • Overview
  • Rotating Objects by Updating the Euler Angles Smoothly Through Code

Was this helpful?

  1. Translate, Rotate, and Scale
  2. Rotate

Updating Rotation Through Script Code

PreviousRotating Game Objects in the EditorNextUsing the Transform.Rotate() Function

Last updated 4 years ago

Was this helpful?

Overview

When the game is running, you will not be able to rotate objects as you can within the editor, so one way to update the angle an object is facing is by updating the object's Euler angles.

The Euler angles are stored in a vector3 variable with read-only parts, so you cannot update just one axis at a time through transform.eulerAngles.y = 90.0F or something similar. You need to use the keyword new followed by a complete Vector3. Here's an example:

// Rotates the object directly to 90 degrees right
transform.eulerAngles = new Vector3(0, 90.0F, 0);

Here, the object rotates right by 90 degrees, making it face right. If it starts at (0,0,0), it appears to jump to facing right in one frame and stays in that direction until it's rotated again.

After rotating, the Transform component will look like this in the Inspector:

Rotating Objects by Updating the Euler Angles Smoothly Through Code

Avoid using Transform.rotation. It is a Quaternion and it takes four coordinates that are different than the Euler angles shown in the Transform component.

Example codes:

  • Each example should be placed within the Update() or FixedUpdate() function so that it runs once per frame

  • Each example rotates the object it is attached to in the positive direction of the y-axis (clockwise)

transform.eulerAngles += new Vector3(0, 1.0F, 0) * Time.deltaTime;

The code above:

  1. Accesses the object's transform and then its rotation in Euler angles

  2. Uses the += shortcut to add values to itself

  3. new Vector3(float x, float y, float z) is a way to tell it to add the matching variables

transform.eulerAngles += Vector3.up * Time.deltaTime;

The code above:

  1. Accesses the object's transform and then its rotation in Euler angles

  2. Uses the += shortcut to add values to itself

  3. Time.deltaTime adjusts for varying computer speeds

Time.deltaTime adjusts for varying computer

Vector3.up is a for Vector3(0.0F, 1.0F, 0.0F); Up is used here because we are spinning it on its y-axis "rod"

speeds
shortcut