15

I can use the following code to rotate object using accelerometer.

transform.rotation = Quaternion.LookRotation(Input.acceleration.normalized, Vector3.up);

But i would like to rotate object like for example screen is rotating - 0, 90, 180 and 360 degrees. How can I do it using Unity 3D?

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
Artur
  • 153
  • 1
  • 1
  • 5

4 Answers4

37

You can use transform.rotation like this:

transform.rotation = new Quaternion(rotx, roty, rotz, rotw);

OR

You can use transform.Rotate like this:

transform.Rotate(rotx, roty, rotz);

Documentation for Quaternion

Documentation for transform.rotation

Example for Rotating screen with accelerometer input:

float accelx, accely, accelz = 0;

void Update ()
{
    accelx = Input.acceleration.x;
    accely = Input.acceleration.y;
    accelz = Input.acceleration.z;
    transform.Rotate (accelx * Time.deltaTime, accely * Time.deltaTime, accelz * Time.deltaTime);
}

If you want to rotate the object to a specific angle use:

float degrees = 90;
Vector3 to = new Vector3(degrees, 0, 0);

transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, to, Time.deltaTime);

This will rotate 90 degrees around the x axis.

Eliasar
  • 1,067
  • 1
  • 7
  • 18
user5819
  • 534
  • 4
  • 11
  • I din't have enough reputation to post more than 2 links but just google transform.Rotate if you need to read some documentation on that too. – user5819 Feb 21 '15 at 16:50
  • It still doesn't give me a solution for this problem. I don't understand your explanation. – Artur Feb 21 '15 at 20:13
  • Explain what exactly do you want to do – user5819 Feb 22 '15 at 10:16
  • "example screen is rotating - 0, 90, 180 and 360 degrees" on which axis (x, y or z)? Using accelerometer input ? – user5819 Feb 22 '15 at 10:19
  • I would like to auto-rotate one object in my scene using accelerometer as i said before. Do you know how screen auto rotation is working? I would like to do the same with only one object on my scene. – Artur Feb 22 '15 at 14:09
  • You mean from landscape to portrait and vice versa ? – user5819 Feb 22 '15 at 14:11
  • Btw to rotate the screen you rotate the camera and camera is a gameobject like any other and work in the same way. So it don't matter what gameobject you rotate, the code is the same. – user5819 Feb 22 '15 at 14:13
  • Yes I mean from portrait to landscape and vice versa. I know there is option in unity to rotate all the app, but I need to rotate only 1 object. – Artur Feb 22 '15 at 17:02
  • Then rotate just 1 object unsing the transform.eulerAngles to rotate it to the desired angle – user5819 Feb 23 '15 at 13:10
  • Unity Docs Link: https://docs.unity3d.com/ScriptReference/Transform.Rotate.html – Chaitanya Estarla Jun 09 '23 at 11:53
5

In order to rotate your game object on its own

int _rotationSpeed = 15;

void Update () {

    // Rotation on y axis
    // be sure to capitalize Rotate or you'll get errors
    transform.Rotate(0, _rotationSpeed * Time.deltaTime, 0);
}
Community
  • 1
  • 1
4

If you are looking to add this to a game object where you can drop the game object into the script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TheNameOfYourScriptHere : MonoBehaviour
{
    public float speed = 100;
    
    public GameObject yourgameobject;
    
    void Update()
    {
          yourgameobject.transform.Rotate(0, speed * Time.deltaTime, 0);
    }
}

Please note this rotation is faster so you can see it better in action.

davidsbro
  • 2,761
  • 4
  • 23
  • 33
Olney1
  • 572
  • 5
  • 15
-1

You can rotate your object via just adding below line in your .cs script.

transform.Rotate(Vector3.up,Here you can put horizonatl speed);

Ramanand Yadav
  • 309
  • 3
  • 3