- Home /
Limiting rotation of object, specifically using scroll wheel
My most recent brain fart has cost me an evening's worth of game development! It is driving me crazy at this point!
Simply put, my character (player) is holding a box and I am trying to make it so that when the user rolls the mouse wheel forward, the box opens (to a max of 45 degs) and when they roll it back, it closes (back to zero).
I have my current script attached to an empty game object which acts as the hinge's pivot point (this gameobject itself is parented under the player character). The hinge object's local rotation transform is (0,0,0) when closed and should be (45,0,0) when open. Obviously because the character can move around, the world space hinge rotations are all over the shop.
Now, what I have cobbled together works to a point, but the hacky 'catch' I've coded to stop the hinge going past its limits is making me cringe, it is messy and not quite what I'm after. It'll do until I can find a better way though. There has to be a better way. Educate me purleeease, I beg of you!!
void Update ()
{
//Get the current rotation of the empty 'hinge' gameobject this script is attached to
float currentRot = transform.localEulerAngles.x;
//Obtain mouse wheel input adjusting for sensitivity this speed works well for the
//speed and feeling of the hinge
float mouseWheel = Input.GetAxis("Mouse ScrollWheel") * speed;
//Clamp mouse wheel speed (to try to stop hinge going to far when user is spinning wheel fast)
float mouseWheelClamped = Mathf.Clamp(mouseWheel, -10, 10);
//Only move hinge if user is moving the scrollwheel
if (mouseWheel != 0){
transform.Rotate(-mouseWheelClamped, 0 , 0, Space.Self);
}
//Really hacky way to try to rotate hinge back towards it's max open angle cos it goes to far. Second condition is there because rotation flips from 0 to 360 degs (euler flipping i hate you)
if (currentRot > 45 && currentRot <= 275)
{
transform.Rotate(-settleSpeed * Time.deltaTime, 0 , 0, Space.Self);
}
//Same smelly hacky way to try to rotate hinge back towards it's min or closed angle
if(currentRot > 275 || currentRot < 0)
{
transform.Rotate(settleSpeed * Time.deltaTime, 0 , 0, Space.Self);
}
}
Huge love to anyone who can throw me a bone. I know I'm over thinking this BIG TIME!
Answer by whydoidoit · Mar 20, 2013 at 09:33 AM
Here's my function for clamping an angle between two values:
float ClampAngle(float angle, float min, float max)
{
bool inverse = false;
var tmin = min;
var tangle = angle;
if(min > 180)
{
inverse = !inverse;
tmin -= 180;
}
if(angle > 180)
{
inverse = !inverse;
tangle -= 180;
}
var result = !inverse ? tangle > tmin : tangle < tmin;
if(!result)
angle = min;
inverse = false;
tangle = angle;
var tmax = max;
if(angle > 180)
{
inverse = !inverse;
tangle -= 180;
}
if(max > 180)
{
inverse = !inverse;
tmax -= 180;
}
result = !inverse ? tangle < tmax : tangle > tmax;
if(!result)
angle = max;
return angle;
}
This was just what I needed sir, I cannot thank you enough. You helped me out of a hole there my good man! I shall post my complete working code here should anyone fall into the same trap!
Answer by RowleyBirkin · Mar 21, 2013 at 04:37 AM
Here is my complete working clamped hingeScript for those that fall into the same trap as I. Huge thanks to whydoidoit! I'll tell ya why you do it, cos you're damn good at it sir!
using UnityEngine; using System.Collections;
public class HingeScriptClamp : MonoBehaviour {
private float speed = 25;
private float currentRot;
// Update is called once per frame
void Update ()
{
//Obtain mouse wheel input
float mouseWheel = Input.GetAxis("Mouse ScrollWheel") * speed;
if(mouseWheel != 0)
{
//Increment rotation by mouseWheel value
currentRot = currentRot - mouseWheel;
//Clamp it to between 0 and 45 degs (Thanks whydoidoit!!!)
currentRot = ClampAngle(currentRot, 0, 45);
//Apply rotation
transform.localEulerAngles = new Vector3(currentRot, 0, 0);
}
}
float ClampAngle(float angle, float min, float max)
{
bool inverse = false;
var tmin = min;
var tangle = angle;
if(min > 180)
{
inverse = !inverse;
tmin -= 180;
}
if(angle > 180)
{
inverse = !inverse;
tangle -= 180;
}
var result = !inverse ? tangle > tmin : tangle < tmin;
if(!result)
angle = min;
inverse = false;
tangle = angle;
var tmax = max;
if(angle > 180)
{
inverse = !inverse;
tangle -= 180;
}
if(max > 180)
{
inverse = !inverse;
tmax -= 180;
}
result = !inverse ? tangle < tmax : tangle > tmax;
if(!result)
angle = max;
return angle;
}
}
@rowleybirkin, you should mark @whydoidoit's answer as correct.