i have a maze that rotates according to user inputs but now i want to put limitations on the rotation so the marble in the maze won’t fall out or that the maze won’t flip upside down.
I thought about having an invisible plane under the maze and have a tag attached to it and use it in the OnTriggerEnter function. when the maze collided with the plane i simply set the rotateSpeed to 0 however then I can’t move the maze at all after that…
another way was to try to use the eulerAngles but i’m a little confused when it comes to that,i understand that i’ll probably need a maxAngle and check if it goes past it.
i’m coding in c# so any push in the right and best way to do this would be greatly appreciated
thanks! that helped, but only with the up and down key inputs…
its still doesn’t stop rotating in the left and right direction and even when i change the angles of rotation in the y direction it seems like nothing is actually changing…
using UnityEngine;
using System.Collections;
public class limitRotate : MonoBehaviour {
public float rotateSpeed = 50;
public float minimumX = -45F;
public float maximumX = 45F;
public float minimumY = -45F;
public float maximumY = 45F;
float rotationX = 0F;
float rotationY = 0F;
// Use this for initialization
void Start ()
{
}
public static float ClampAngle (float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp (angle, min, max);
}
void Update ()
{
rotationY += Input.GetAxis("Vertical") * rotateSpeed * Time.deltaTime;
rotationY = ClampAngle (rotationY, minimumY, maximumY);
rotationX+= -Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.right);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.forward);
transform.rotation= yQuaternion * xQuaternion;
}
}
i changed the clamp limits to 30 so now it resets everything when it reaches that angle but when the maze starts if the input is “down” the maze doesn’t tilt as if it already reached its limit…
also is there a way to make the reseting to the original position smoother?
It is probably easiest to use the physics engine for something like this. I’ve attached a prefab you should be able to use, perhaps with a little modification. Just replace the flattened cube object with your maze. The way it works is by having two rigidbodies with hinge joints. One turns only in the X axis and the other only in Z. The hinge joints are limited to a narrow range of angles and have springs to reset them to a neutral position after movement. Then, there is a script that applies torque in the X and Z directions when the arrow keys are pressed.
thank you so much! thats exactly what i was going for
however, it won’t let me download what you posted…is there another way I could get it? if not the information you gave me is very helpful too so I can learn more about hinges and springs since I have never used them before in unity.
the prefab you sent me help a lot and i made a few changes so now it works for my maze as well. however now the marble that i had rolling around in the maze according to the tilting of the maze isn’t following the movement of the maze and isn’t even colliding with the maze. it just goes through it. i’m wondering if it has to do with the parenting of the objects. I have a prefab called mazeRocker which is the parent of the ZRocker and XRocker and ZRocker is also the parent of the maze itself (just like in your prefab but with the cube). the maze is a rigidbody (uses gravity and kinematics) and has a box collider and the sphere is its own independent object and is a ridigbody (uses only gravity) as well and has a sphere collider. this worked before i added the joint hinges. the sphere rolled around the maze according to the tilting and collided with its walls and floor. now it static and goes through the maze. I tried adding box colliders to the Xrocker and Zrocker but that didn’t help the situation.
any advice? i don’t know whats causing the problem since i have the colliders set up…
i think i figured out what’s causing the problem… i think its the script that uses rigidbody.addTorque in order to tilt the maze. i don’t think its moving the colliders. when i add my original script to it the colliders work but the joint hinges that limit the maze rotation dont…
any help with how i can fix this problem with the addTorque function?
the sphere is a rigidbody that uses gravity and has a sphere collider. it isn’t controlled by any script, only the maze it. the marble should just roll about the maze due to gravity caused by the maze tilting. im currently using the mazeRocker script that you attached to your rocker prefab. whenever i use this script the tilting and everything works but the ball is static and goes through the maze. when i use my original script (that doesn’t limit the rotation which was my original problem) the ball interacts with the maze accordingly. i don’t know why it is doing it since i have a rigidbody and box collider on the maze…
i noticed that in the prefab rocker the cube isn’t a rigidbody, but since it isn’t when i place a sphere on it it just tilts where the sphere is and stays there and won’t move according to the key inputs. so when i add a rigidbody to the cube the sphere just doesnt move and goes through the maze. how can i avoid this problem using hinge joints?
so i realized that hinge joints were not the solution to my problems and wrote a short script imitating the behavior of hinge joints using the idea of the mathf.Clamp function to limit the rotation.
hope this help anyone who needed script that would limit an objects rotation but also tilt it back to its original position if there is not input.
using UnityEngine;
using System.Collections;
public class useClamp : MonoBehaviour {
public float rotateSpeed = 50;
public float minimumX = -90F;
public float maximumX = 90F;
public float minimumY = -90F;
public float maximumY = 90F;
float rotationX = 0F;
float rotationY = 0F;
// Use this for initialization
void Start ()
{
}
public static float ClampAngle (float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp (angle, min, max);
}
void FixedUpdate ()
{
if(Input.anyKey == false) //no key was pressed
{
if(rotationY > 0) //up key was pressed
{
if(rotationY != 0 ) //while it doesn't reach it original position
{
rotationY = rotationY - (float)0.2; //decraese rotationY
}
}
if(rotationY < 0) //down key was pressed
{
if(rotationY != 0) //while it doesn't reach its original position
{
rotationY = rotationY + (float)0.2; //increase rotationY
}
}
if(rotationX > 0) //left key was pressed
{
if(rotationX != 0) //while it doesn't reach its original position
{
rotationX = rotationX - (float)0.2; //decrease rotationX
}
}
if(rotationX < 0 ) //right key was pressed
{
if(rotationX != 0) //while it doesn't reach its original position
{
rotationX = rotationX + (float)0.2; //increase rotationX
}
}
}
rotationY += Input.GetAxis("Vertical") * rotateSpeed * Time.deltaTime;
rotationY = ClampAngle (rotationY, minimumY, maximumY);
rotationX+= -Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.right);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.forward);
transform.rotation= yQuaternion * xQuaternion;
}
}
This is probably too late for you to do anything about it, but I ran into the same problem recently. I solved it by making the ball a child of the maze. That way it doesn’t get thrown off by the tilting.
Wow, thank you for that. I’ve been struggling with this problem, as well, and was also planning on going with a physics solution, but I think this is really what’s best for responsiveness. Torque ends up being just too floaty. This script acts really nicely.