Clamp Rotation Unity Code Example


Example 1: unity clamp rotation


float rotationX = 0;
float rotationY = 0;
// you might also have some rotation speed variable
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 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 3: how to chagne rotation in unity


transform.eulerAngles = new Vector3(0f, 180f, 0f); // rotates 180 degrees

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable