How to restrict 2d rotation

Hey,
Sorry for the probably easy fix question but how do you add restriction on how far a object can rotate?
My code is at follows, its simple cause this is the first time I’ve tried Unity but I do have experience using Java.

public class rotate : MonoBehaviour
{
public float speed = 0.01f;

void Update()
{

if (Input.GetKey(KeyCode.RightArrow))
{
transform.Rotate(0f,0f,speed*-1);
}

if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Rotate(0f, 0f, speed);
}
}
}

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

To solve your problem:

  • keep a float that is the angle, adjust that, clamp that

  • “drive” the float out to the rotation using Quaternion.Euler( 0, angle, 0) (around whatever axis you want)

More on this notion: Turret aiming/rotating:

ALSO: if you are adjusting a quantity yourself, ALWAYS multiply by Time.deltaTime or else your game will behave differently at different frame rates.