unity c# rotation code example
Example 1: unity clamp rotation
float rotationX = 0;
float rotationY = 0;
void Update() {
rotationX += Input.GetAxis("Vertical") * Time.deltaTime;
rotationX = Mathf.Clamp(rotationX, minRotationX, maxRotationX);
rotationY += Input.GetAxis("Horizontal" * Time.deltaTime;
transform.rotation = Quaternion.Euler(rotationX, rotationY, 0);
}
Example 2: how to change rotate with script unity
var rotationVector = transform.rotation.eulerAngles;
rotationVector.z = 0;
transform.rotation = Quaternion.Euler(rotationVector);
Example 3: 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 4: 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);
}
}