Hello, I am in dire need of help here. What I’m trying to achieve is some kind of limit to an object’s rotation. I’m working on a game about table soccer where roulettes aren’t allowed on some tables, but to limit the rotation of the handles just seems impossible. I’ve looked at other questions but I couldn’t implement none of them in it. The handles right now are just 1 floating rectangle in the middle of the field, for testing reasons. The code is as follows:
using UnityEngine;
using System.Collections;
public class ctrl : MonoBehaviour {
public GameObject handle1;
public GameObject handle2;
public GameObject handle3;
public GameObject handle4;
public float handler = 0.00f;
// Update is called once per frame
void Update () {
//Everything needs a limit; even the rotation of the handles.
//The first handle's limits
if (handle1.transform.rotation.x < -80)
{
handle1.transform.rotation = Quaternion.Euler(-80,0,0);
Debug.Log("gud1");
}
if (handle1.transform.rotation.x > 110)
{
handle1.transform.rotation = Quaternion.Euler(110,0,0);
}
//The second handle's limits
if (handle2.transform.rotation.x < -80)
{
handle2.transform.rotation = Quaternion.Euler(-80,0,0);
}
if (handle2.transform.rotation.x > 110)
{
handle2.transform.rotation = Quaternion.Euler(110,0,0);
}
//The third handle's limits
if (handle3.transform.rotation.x < -80)
{
handle3.transform.rotation = Quaternion.Euler(-80,0,0);
}
if (handle3.transform.rotation.x > 110)
{
handle3.transform.rotation = Quaternion.Euler(110,0,0);
}
//The fourth handle's limits
if (handle4.transform.rotation.x < -80)
{
handle4.transform.rotation = Quaternion.Euler(-80,0,0);
}
if (handle4.transform.rotation.x > 110)
{
handle4.transform.rotation = Quaternion.Euler(110,0,0);
}
handle1.transform.Rotate(handler,0,0);
handle2.transform.Rotate(handler,0,0);
handle3.transform.Rotate(handler,0,0);
handle4.transform.Rotate(handler,0,0);
//Left
if (Input.GetAxis("Mouse X")<0)
{
handler += 2.50f;
Debug.Log("Mouse moved left");
}
if (handler > 0)
{
handler -= 1.25f;
}
if (handler > 5.00f)
{
handler = 5.00f;
}
//Right
if (Input.GetAxis("Mouse X")>0)
{
handler -= 2.50f;
Debug.Log("Mouse moved right");
}
if (handler < 0)
{
handler += 1.25f;
}
if (handler < -5.0f)
{
handler = -5.0f;
}
}
}
I’ve tried that but nothing seems to change. I’ve also tried transform.rotation.x but it just gives errors out because apparently that statement is read-only and can’t be edited separatedly.