# REQ - Limiting Jamie's rotate with keyboard script

Jamie created a cool little .js script that rotates an object with keyboard input. It goes like this …

``````/// This behavior script rotates an object by the mouse delta

// All public variables are visible in the inspector and can be edited there.
var speed = 0.1;

function Update () {
x= Input.GetAxis ("Horizontal") * speed;
y= Input.GetAxis ("Vertical") * speed;

// Rotate around works in radians
transform.RotateAround (Vector3.fwd, -x);
transform.RotateAround (Vector3.left, -y);

}
``````

How would I limit the rotation to only 5? (five degrees) up or down in the two planes?

``````/// This behavior script rotates an object by the mouse delta

// All public variables are visible in the inspector and can be edited there.
var speed = 10;
var x = 0.0;
var y = 0.0;

function Update () {
† †x += Input.GetAxis ("Horizontal") * speed;
† †y += Input.GetAxis ("Vertical") * speed;

x = Mathf.Clamp (x, -90.0, 90.0);
y = Mathf.Clamp (y, -10.0, 10.0);

transform.rotation = Quaternion.identity;

† † transform.Rotate (y, x, 0);

// Use the following two lines instead of transform.Rotate (y, x, 0); if you
//want a fps style rotation
// transform.Rotate (0, x, 0, Space.World);
// transform.Rotate (y, 0, 0);
}
``````

In the standard assets there is also a script which does that (MouseLook.cs) but it is written in C# unfortunately.

Thanks much Joe! Each time I get someone’s help on the scripting end, I actually figure out more and more of the language, syntax etc.

Amazing! I was about ready to ask the same question!

Thanks for the solution as well…

I tweaked this to affect the y and z axis, not the x axis (so I simulate pitch and roll but not yaw). Cool! Now I’m trying to, if an object falls below a y value of 35, destroy it and the create it again at a certain world position (for sake of argument lets say x35, y45, z55). Is this somewhat along the right path?

``````/// a script that will reinstatiate an object if it falls off the platform

function Update () {
if (transform.position.y < -35) {
Destroy (gameObject);
transform.Vector3 position (x, y, z);
}
}
``````

I think the transform.Vector3 position (x, y, z); is wrong.

``````function Update () {
† †if (transform.position.y < -35) {
† †† †transform.position = Vector3 (5, 10, 1);
† †}† †
}
``````

Why do you want to recreate it?
If you want to recreate it, you have to use the Instantiate function but that probably isn’t what you want because:

Instantiate (gameObject);
Destroy (gameObject);

Is the same as doing nothing basically, because you first clone the object, then delete it. So you end up with the same number of objects.

Yes, I thought this after posting it but got busy last night. Rather than destroy and recreate I did mean just to move it to another location.

If you don’t want to manually type the coordinates into the script, you could save the object’s initial position at the start of the scene. I’m using something similar to this in my marble game:

``````var respawnPosition: Vector3;

function Awake() {
// save original position
respawnPosition=transform.position;
}

function Update () {
if (transform.position.y < -35) {
transform.position = respawnPosition;
}
}
``````