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
  • Controlling Speed
  • Vector3 Shortcuts

Was this helpful?

  1. Translate, Rotate, and Scale
  2. Rotate

Using the Transform.Rotate() Function

Overview

When the game is running, you will not be able to rotate objects as you can within the editor, so one way to rotate objects smoothly is with the Transform.Rotate() function.

It updates the Euler angles of the object and can be used incrementally to make the movement appear smooth.

Here is an example of a player rotate script using Transform.Rotate() to rotate a character when a key is pressed:

using UnityEngine;

public class PlayerRotate : MonoBehaviour
{
    void Update()
    {
        // Press Left Arrow to rotate the object left/counter-clockwise
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.Rotate(0, -1.0F, 0);
        }

        // Press Right Arrow to move the object right/clockwise
        if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.Rotate(0, 1.0F, 0);
        }
    }
}

Controlling Speed

Commands in the Update() function run once per frame, meaning speed changes based on the computer's frame rate.

Multiplying by Time.deltaTime adjusts the speed to be about the same, even with different frame rates. Adding a speed variable helps you adjust the speed it moves.

Here's the same example with these adjustments:

using UnityEngine;

public class PlayerMotor : MonoBehaviour
{
    public float moveSpeed = 10;



    void Update()
    {
        // Press Left Arrow to rotate the object left/counter-clockwise
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.Rotate(new Vector3(0, -1.0F, 0) * moveSpeed * Time.deltaTime);
        }

        // Press Right Arrow to move the object right/clockwise
        if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.Rotate(new Vector3(0, 1.0F, 0) * moveSpeed * Time.deltaTime);
        }
    }
}

Vector3 Shortcuts

Instead of remembering and typing a long piece of code like the one below:

new Vector3(0, 1.0, 0)

Unity has short cuts like this one that you only have to remember a direction:

Vector3.up

Here's the same example with these shortcuts:

using UnityEngine;

public class PlayerMotor : MonoBehaviour
{
    public float moveSpeed = 10;
    
    void Update()
    {
        // Press Left Arrow to rotate the object left/counter-clockwise
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.Rotate(Vector3.down * moveSpeed * Time.deltaTime);
        }

        // Press Right Arrow to move the object right/clockwise
        if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.Rotate(Vector3.up  * moveSpeed * Time.deltaTime);
        }
    }
}
PreviousUpdating Rotation Through Script CodeNextScale

Last updated 4 years ago

Was this helpful?