Problems with limiting angles
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.
Answer by Maruder · Sep 14, 2015 at 02:10 AM
@Marcos42563 From what I see in the code, it appears to me you are trying to rotate the paddle about the x-axis and are trying to constrain limits at how far each handle can turn or rotate.
I don't know if this will help but I do have code that you can probably look over that deals with limiting angle rotations here:
public float turn_speed;
private float turn_velocity;
public float minAngle,maxAngle;
//angles to modify for turning.
private float x,y,z;
void Start()
{
//set x,y,z angles to modify
x = transform.eulerAngles.x;
y = transform.eulerAngles.y;
z = transform.eulerAngles.z;
//convert frames per second to units per second movement.
turn_velocity = turn_speed * Time.deltaTime;
}
void Update()
{
RotateArrow ();
LaunchBall ();
}
void RotateArrow()
{
//right
if(Input.GetKey(KeyCode.D))
{
z -= turn_velocity;
}
//left
if(Input.GetKey (KeyCode.A))
{
z += turn_velocity;
}
//restrict angle of rotation
z = Mathf.Clamp (z, minAngle, maxAngle);
//set rotate Angle to game Object
transform.eulerAngles = new Vector3 (x, y, z);
}
In the unity editor, you can set the minAngle and maxAngle each handle can turn as they are public. Furthermore, if you look at the line under the comment //restrict angle of rotation, you can use Mathf.Clamp(angle to rotate about,min Angle,max Angle) to put constraints on how much a paddle can rotate.
Instead of rotating about the z-axis, you can rotate about the x-axis where your following code may look like this:
x = Mathf.Clamp(x,minAngle,maxAngle);
Your answer
Follow this Question
Related Questions
Why won't my model rotate up on X axis? 1 Answer
How to make a gameObject's Y rotation being another gameObjects' X rotation 1 Answer
.rotation doesn't seem to work, can't shoot projectile at mouse coordinates 0 Answers
Searching a function to get the rotation of one Vector3 in relation to another 1 Answer
How to deal with stupid rotation system? 2 Answers