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!