Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by OGNE · May 29, 2019 at 07:08 AM · rotation3dquaternioneuleranglesclamped rotation

Clamped Rotate 3D Object on Y and Z axis only with OnMouseDrag()

Hello guys I've been struggling in rotating a gameobject on y and z axis only with Y value clamped between (-90 & -120) & Z value between (0 & -10). I've tried many online solutions with eulerangles and rotation and still stuck. I want to rotate 3D model with drag so I thought that OnMouseDrag would work. Any help would be really appreciated. Thanks.

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
1

Answer by xxmariofer · May 29, 2019 at 08:55 AM

test this code, is not optimised but it should work :) ALSO take into account this, dont work with euler angles with negative values, since for unity -110 is 250 so you will have a lot of issues comparing values (you will probably need to use the positive values)

     void Update()
     {
         if (Input.GetMouseButtonDown(0)) lastPosition = Input.mousePosition;
     }
 
     Vector2 lastPosition;
     public float speed = 1f;
     private void OnMouseDrag()
     {
         Debug.Log(transform.rotation.eulerAngles.y);
 
         if (Input.mousePosition.x > lastPosition.x + 10)
             transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x,Mathf.Clamp( transform.rotation.eulerAngles.y + speed, 0, 30), transform.rotation.eulerAngles.z);
         else if(Input.mousePosition.x < lastPosition.x - 10)
             transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, Mathf.Clamp(transform.rotation.eulerAngles.y - speed, 0, 30), transform.rotation.eulerAngles.z);
 
         if (Input.mousePosition.y > lastPosition.y + 10)
             transform.rotation = Quaternion.Euler(Mathf.Clamp(transform.rotation.eulerAngles.x + speed, 0, 10), transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);
         else if (Input.mousePosition.y < lastPosition.y - 10)
             transform.rotation = Quaternion.Euler(Mathf.Clamp(transform.rotation.eulerAngles.x - speed, 0, 10), transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);
     }
Comment
Add comment · Show 1 · 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 OGNE · May 29, 2019 at 09:30 AM 0
Share

Thanks. I'll try it and let you know.

avatar image
0

Answer by OGNE · May 29, 2019 at 11:05 AM

I changed your code a little bit according to my values but there is still some error in Z axis. Its quickly snaps between the values used in code but tha's not the case with Y axis. The y axis is working perfectly. My 3D model is set as (0,270,360) and it is supposed to go to (0,230,340). Here is the code I changed

 Vector2 lastPosition;
 public float speed = 1f;

 private void OnMouseDrag()
 {
     //Debug.Log(transform.rotation.eulerAngles.y);

     if (Input.mousePosition.x < lastPosition.x + 10)
     {
         print("condition 1");
         transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, Mathf.Clamp(transform.rotation.eulerAngles.y + speed, 230, 270), transform.rotation.eulerAngles.z);
     }

     else if (Input.mousePosition.x > lastPosition.x - 10)
     {
         print("condition 2");
         transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, Mathf.Clamp(transform.rotation.eulerAngles.y - speed, 230, 270), transform.rotation.eulerAngles.z);
     }


     if (Input.mousePosition.y > lastPosition.y + 10)
     {
         print("condition 3");
         transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, Mathf.Clamp(transform.rotation.eulerAngles.z - speed, 340, 360));//org
     }

     else if (Input.mousePosition.y < lastPosition.y - 10)
     {
         print("condition 4");
         transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, Mathf.Clamp(transform.rotation.eulerAngles.z - speed, 360, 340));//org
     }
 }
 void OnMouseUp()
 {
     transform.rotation = Quaternion.Euler(0, 270, 360);
 }
Comment
Add comment · Show 6 · 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 xxmariofer · May 29, 2019 at 11:49 AM 0
Share

it is not working because you are doing "- speed" in both if/elses you need to change it to + speed

avatar image OGNE xxmariofer · May 29, 2019 at 12:24 PM 0
Share

if i do + speed it flickers.

avatar image xxmariofer OGNE · May 29, 2019 at 12:35 PM 0
Share

only change to + speed in the condition3, and change in the condition 4 the 340 to be before the 360 (since the first value is the $$anonymous$$ and the second the max)

Show more comments

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

164 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

My Obstacle Rotations Are Not Correct 0 Answers

Code to rotate around a local axis until local Y is 0? 0 Answers

Why Is Rotation Different For RectTransform? 0 Answers

How to set a single-axis rotation and not change the others axes 1 Answer

how to change player rotation in topdown view game 2 Answers


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