# How to limit the rotation of an object

Hello to all guys
I am new here because I am new to the world of unity.
I write to u because some weeks ago I beginned studing scripts and how they works.
I am reading the Unity 3.x Game Developement essential and following the exercises and tutorials.

During one of these, I decided to create a turret that rotate on X axis with a limit on its range of rotation (please see the attached image to have a best idea of what I am writing about).

So in order to make my project real in the monodevelop first I create the script to rotate the cannon using uparrow and down arrow, here u are what I did in C#:

``````public class cannon : MonoBehaviour {

public float m_RotationSpeed =  15;

void Start () {
}

void Update ()
{

float xcontrol = m_RotationSpeed * Time.deltaTime;
float xrot = transform.rotation.eulerAngles.x;

if(Input.GetKey(KeyCode.UpArrow)
{
transform.Rotate(xcontrol,0,0);
}

if(Input.GetKey(KeyCode.DownArrow))
{
transform.Rotate(-xcontrol,0,0);
}
}
}
``````

Then I create a variable xrot in order to register the angle of the x axis hoping to use it to limit the range of the rotation with an “if” function, but this way seems to be really wrong after hunderd of attempts.

So, I am now at a blind point (a poor point in effect) because I don’tknow what I need to do in order to realize mi porject.

Searching on the web I found a lot of scripts that use a misterous (misterius for me) function called Mathf.Clamp.

I am a true beginner in scripting but I really want to understand and imporve so I am here to ask for your help.

I need someone that would like to write the script for me and explain the functions and how they works. Please I am a true beginner so use the most simple way to explian because it’s easy for me get confused, sorry aboutthat. I prefer C# examples, I have never work using Java.

I know it’s a boring issue but for me it’s very important.

Thank you for the attention and hope to hear form you soon!
Best regards

Matthew

This was developed from the standard assets script MouseOrbit.

First the current rotations are stored in Start.

Then the cached value of the y rotation is modified by the inputs.

Then that value is processed by the method ClampAngle. This makes sure the cached value is between -360 and 360. Then that value is clamped between the limit values before being returned.

Then a new rotation is constructed using the cached values as euler angles.

Finally that rotation is given to the gameObjects transform. It is important to note this is in world space. If you want to use local space, use transform.localRotation

Here is the C# version :

``````using UnityEngine;
using System.Collections;

public class ClampingRotation : MonoBehaviour
{
public float ySpeed = 250.0f;

public float yMinLimit = -80f;
public float yMaxLimit = 80f;

private float x = 0.0f;
private float y = 0.0f;
private float z = 0.0f;

void Start()
{
x = transform.eulerAngles.x;
y = transform.eulerAngles.y;
z = transform.eulerAngles.z;
}

void Update()
{
if ( Input.GetKey(KeyCode.UpArrow) )
{
y += ySpeed * Time.deltaTime;
}

if ( Input.GetKey(KeyCode.DownArrow) )
{
y -= ySpeed * Time.deltaTime;
}

y = ClampAngle( y, yMinLimit, yMaxLimit );

Quaternion newRot = Quaternion.Euler( x, y, z );

transform.rotation = newRot;
}

float ClampAngle( float angle, float min, float max )
{
if ( angle < -360 )
angle += 360;
if ( angle > 360 )
angle -= 360;

return Mathf.Clamp( angle, min, max );
}
}
``````

And for future readers, here is the uJS version :

``````#pragma strict

var ySpeed : float = 250.0;

var yMinLimit : float = -80;
var yMaxLimit : float = 80;

private var x : float = 0.0;
private var y : float = 0.0;
private var z : float = 0.0;

function Start()
{
x = transform.eulerAngles.x;
y = transform.eulerAngles.y;
z = transform.eulerAngles.z;
}

function Update()
{
if ( Input.GetKey(KeyCode.UpArrow) )
{
y += ySpeed * Time.deltaTime;
}

if ( Input.GetKey(KeyCode.DownArrow) )
{
y -= ySpeed * Time.deltaTime;
}

y = ClampAngle( y, yMinLimit, yMaxLimit );

var newRot : Quaternion = Quaternion.Euler( x, y, z );

transform.rotation = newRot;
}

function ClampAngle( angle : float, min : float, max : float ) : float
{
if ( angle < -360 )
angle += 360;
if ( angle > 360 )
angle -= 360;

return Mathf.Clamp( angle, min, max );
}
``````

=======================================================================================

Always check the Unity Scripting Reference : Unity - Scripting API: Mathf.Clamp

There are 2 examples, for floats and for integers.

Description : Clamps value between a minimum value and maximum value, and returns a value

For example :

``````float myValue = 10.5;
float clampedValue = Mathf.Clamp( myValue, 1.5, 3.5 );
``````

Here it is feeding myValue into the Clamp command, with the returned value clamped between a minimum of 1.5 and a maximum of 3.5 . So in this example, clampedValue would be 3.5

If myValue was 2.1, clampedValue would return 2.1

If myValue was 0.9, clampedValue would return 1.5

Always check the Unity Scripting Reference. For examples in C#, find the tab on the right hand side, just above the example code. Where it says Javascript, click on this and a drop-down box appears. Now you can select C# example scripts =]

To learn about rotations, I strongly recommend you check out the examples at UnityGems : unitygems.com - unitygems Resources and Information.

With the Unity Standard assets, there is a script called MouseOrbit. Import this script and have a look : Assets > Import Package > Scripts

just like how they have y min and max limits, you could add x min and max limits, then clamp x to these angles after you have calculated the X rotation.