unity transform rotation code example

Example 1: how to change rotate with script unity

var rotationVector = transform.rotation.eulerAngles;
rotationVector.z = 0;  //this number is the degree of rotation around Z Axis
transform.rotation = Quaternion.Euler(rotationVector);
//if you put this in a coroutine and yielding for some amount of time 
//you can have something like a rotating loading icon

Example 2: how to rotate object unity

public Transform Position; // make reference in inspector

Position.Rotate(x, y, z);

Example 3: how to clamp transform.rotation

headRotation = Input.GetAxis("Mouse Y") * speed;
 transform.Rotate(0, 0, -headRotation, Space.Self);
 transform.eulerAngles.y = Mathf.Clamp(transform.eulerAngles.y, -90, 90);

Example 4: transform.rotation - 90 unity

var lookPos = target.position - transform.position; lookPos.y = 0; var rotation = Quaternion.LookRotation(lookPos); rotation *= Quaternion.Euler(0, 90, 0); // this adds a 90 degrees Y rotation var adjustRotation = transform.rotation.y + rotationAdjust; //<- this is wrong! transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);

Example 5: transform.rotation

transform.eulerAngles = new Vector3(0, 0, 180);
transform.localEulerAngles = new Vector3(0, 0, 180);
transform.eulerAngles = new Vector3(0, 0, 180);
transform.localRotation = Quaternion
transform.rotation = Quaternion.Euler(0, 0, 90);

Example 6: unity rotate object c#

using UnityEngine;// Transform.Rotate example
//
// This script creates two different cubes: one red which is rotated using Space.Self; one green which is rotated using Space.World.
// Add it onto any GameObject in a scene and hit play to see it run. The rotation is controlled using xAngle, yAngle and zAngle, modifiable on the inspector.public class ExampleScript : MonoBehaviour
{
    public float xAngle, yAngle, zAngle;    private GameObject cube1, cube2;    void Awake()
    {
        cube1 = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube1.transform.position = new Vector3(0.75f, 0.0f, 0.0f);
        cube1.transform.Rotate(90.0f, 0.0f, 0.0f, Space.Self);
        cube1.GetComponent<Renderer>().material.color = Color.red;
        cube1.name = "Self";        cube2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube2.transform.position = new Vector3(-0.75f, 0.0f, 0.0f);
        cube2.transform.Rotate(90.0f, 0.0f, 0.0f, Space.World);
        cube2.GetComponent<Renderer>().material.color = Color.green;
        cube2.name = "World";
    }    void Update()
    {
        cube1.transform.Rotate(xAngle, yAngle, zAngle, Space.Self);
        cube2.transform.Rotate(xAngle, yAngle, zAngle, Space.World);
    }
}