How to Rotate an Object with Unity 3D

In Unity it's possible to rotate an Object using the power of the script: in the "update" method you can change the rotation's object automatically or by user input.



Rotate an object automatically

Create a new scene (or use an existing one)
Add the object to be rotated
Attach a new script to the object with this code:

using UnityEngine;
using System.Collections;

public class AutoRotate: MonoBehaviour {

 private float xSpeed = 0.0f;
 private float ySpeed = 1.1f;
 private float zSpeed = 1.1f;
 
   // Update is called once per frame
   void Update () {
      // rotate x, y and z with different rotation speed 
      transform.Rotate( xSpeed * Time.deltaTime, ySpeed * Time.deltaTime, zSpeed * Time.deltaTime);
   }
}

Rotate an object by user input (keyboard)

Create a new scene (or use an existing one)
Add the object to be rotated
Attach a new script to the object with this code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class UserRotate : MonoBehaviour {

 private float rotationRate = 3.0f;
 private float h;
 private float v;
 
    void Update (){
      // get the user input
      h = Input.GetAxis("Horizontal");
      v = Input.GetAxis("Vertical");
  
      if (h != 0 || v != 0){
          transform.Rotate (v * rotationRate, -h * rotationRate, 0, Space.World);
      }
    }
}

Rotate an object by user input (touch on android/iOS)

Create a new scene (or use an existing one)
Add the object to be rotated
Attach a new script to the object with this code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class UserRotateTouch : MonoBehaviour {

private float rotationRate = 3.0f;

    void Update () {
        // get the user touch inpun
        foreach (Touch touch in Input.touches) {
            Debug.Log("Touching at: " + touch.position);
   
            if (touch.phase == TouchPhase.Began) {
                Debug.Log("Touch phase began at: " + touch.position);
            } else if (touch.phase == TouchPhase.Moved) {
                Debug.Log("Touch phase Moved");
                transform.Rotate (touch.deltaPosition.y * rotationRate, 
                                 -touch.deltaPosition.x * rotationRate, 0, Space.World);
            } else if (touch.phase == TouchPhase.Ended) {
                Debug.Log("Touch phase Ended");    
            }
        }
    }
}




Comments

  1. Nice tutorial showing how to rotate objects. I'm using this in an AR scene an although I can rotate the object it does not feel real in that there seems to be some kind of loss in relation to the direction my finger drags to the direction the model moves? Any ideas?

    ReplyDelete
    Replies
    1. Hi FastTracker thanks for your comment.
      The mobile version of this example uses the TouchPhase component: you can detect only 2 axes and then rotate your object:

      transform.Rotate(touch.deltaPosition.y * rotationRate, -touch.deltaPosition.x * rotationRate, 0, Space.World);

      The Z axis is static (0 value)

      To make it looks better you should transform 2 directions (from the finger) into a 3-axes Vector or make a "drag'n'drop" style code.

      Or you could use the "keyboard" example (which is using a 3-axes Vector) and map the movement into 3 commands-keys.


      Delete
  2. Great sample code! I have applied this to a 3D model in an augmented reality scene and the model does move but there seems to be some loss in accurate direction movement between how my finger moves and the model responds so makes it feel as though I am nor "really" controlling its movement. Any ideas?

    ReplyDelete
  3. thank you so much ,you really saved me

    ReplyDelete
  4. Your content is nothing short of brilliant in many ways. I think this is engaging and eye-opening material. Thank you so much for caring about your content and your readers.
    best 3d modeling software

    ReplyDelete

Post a Comment