Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by RowleyBirkin · Mar 20, 2013 at 08:51 AM · rotationrotatelimiteulerscrollwheel

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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;
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RowleyBirkin · Mar 21, 2013 at 04:34 AM 0
Share

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!

avatar image whydoidoit · Mar 21, 2013 at 09:41 AM 0
Share

Glad it helped :)

avatar image
0

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;
 }

}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chronos-L · Mar 21, 2013 at 08:31 AM 0
Share

@rowleybirkin, you should mark @whydoidoit's answer as correct.

avatar image RowleyBirkin · Mar 21, 2013 at 08:56 AM 1
Share

ah, right you are! Thanks Chronos-L!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How limit the rotation of an object Or if there is other way to rotate 1 Answer

How to properly reference a Euler? 1 Answer

How do I rotate a game object in 45 degree steps per keystroke? 2 Answers

Object rotation to exact degrees not working 3 Answers

Limiting RotateTowards on two axes 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges