Unity rotate an object code example
Example 1: how to rotate object unity
public Transform Position;
Position.Rotate(x, y, z);
Example 2: unity rotate object c#
using UnityEngine;
{
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);
}
}
Example 3: unity smoothly rotate
private float desiredRot; public float rotSpeed = 250; public float damping = 10; private void OnEnable() { desiredRot = transform.eulerAngles.z; } private void Update() { if (Input.GetMouseButton(0)) { if (Input.mousePosition.x > Screen.width / 2) desiredRot -= rotSpeed * Time.deltaTime; else desiredRot += rotSpeed * Time.deltaTime; } var desiredRotQ = Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, desiredRot); transform.rotation = Quaternion.Lerp(transform.rotation, desiredRotQ, Time.deltaTime * damping); }