- Home /
Question by
Vandos2K01 · Jan 29, 2018 at 04:43 PM ·
rotationtransform.rotationtransform.rotategetkey
How to add limit to object rotation?
I want to limit the object rotation around the x axis.
public float speed = 20;
private float rot;
// Use this for initialization
void Start () {
rot = speed * Time.deltaTime;
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.W)) {
transform.Rotate (rot, 0, 0);
}
if (Input.GetKey (KeyCode.S)) {
if (transform.rotation.x >= -45) { /*HERE*/
transform.Rotate (-rot, 0, 0);
}
}
if (Input.GetKey (KeyCode.A)) {
transform.Rotate (Vector3.up * -speed * Time.deltaTime, Space.World);
}
if (Input.GetKey (KeyCode.D)) {
transform.Rotate (Vector3.up * speed * Time.deltaTime, Space.World);
}
}
}
Comment
Best Answer
Answer by Obsessi0n · Jan 29, 2018 at 06:01 PM
This is what i use to limit rotation with the mouse on the Y axis to 45. You need to clamp the values.
public float minimumVert = -45.0f;
public float maxVert = 45.0f;
public float _rotationX = 0;
public float sensHorizontal = 10.0f;
public float sensVertical = 10.0f;
void Update(){
if (axes == RotationAxis.MouseY)
{
_rotationX -= Input.GetAxis("Mouse Y") * sensVertical;
_rotationX = Mathf.Clamp(_rotationX, minimumVert, maxVert);
float rotationY = transform.localEulerAngles.y;
transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
}
}
Your answer
Follow this Question
Related Questions
Transform.Rotate() is rotating less than given rotation. What's going on? 4 Answers
Rotating game Object. 2 Answers
Have Head Bone Rotate with Camera 2 Answers
Rotation of object 0 Answers