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 /
  • Help Room /
avatar image
0
Question by Der_Kevin · Dec 10, 2015 at 03:40 PM · rotationtransformquaternioneuler

limit Quaternion.Euler rotation

hey! so i got this piece of code when a button gets hold down:

 maze.transform.rotation *= Quaternion.Euler(0,0,40*Time.deltaTime);

with this script snippet a plane is rotating smoothly all the time. but i want to let it stop at lets say 20° even if my button is still gets hold down. how can i do this? thanks!

Comment
Add comment · Show 2
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 imp903 · Dec 10, 2015 at 04:36 PM 0
Share

I'm going to leave this as a comment, because I'm not entirely proficient in this, but have you tried the $$anonymous$$athf.Clamp() method to limit the rotation yet? I believe you can just do this:

 maze.transform.rotation *= Quaternion.Euler(0,0,$$anonymous$$athf.Clamp(40*Time.deltaTime, -20, 20));
 

avatar image Der_Kevin imp903 · Dec 10, 2015 at 05:49 PM 0
Share

thanks. but that also let the plane rotate infinite

2 Replies

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

Answer by akh32 · Dec 11, 2015 at 01:56 PM

I would start by storing your result in a temporary variable, then testing the z-rotation, like so:

 Quaternion t = maze.transform.rotation * Quaternion.Euler(0,0,40*Time.deltaTime);
 
 if(t.euler.z > 20){
 t = Quaternion.Euler(t.euler.x, t.euler.y, 20);
 }
 
 maze.transform.rotation = t;

This assumes that you want a global limit of 20° rotation, and not that you want the maximum amount of rotation provided by this button to be 20°.

Comment
Add comment · 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
0

Answer by mr_duke · Dec 10, 2015 at 05:23 PM

mby

 maze.transform.rotation = Quaternion.RotateTowards(maze.transform.rotation, Quaternion.Euler(0,0,40), Time.deltaTime);

help

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 Der_Kevin · Dec 10, 2015 at 05:51 PM 0
Share

thanks. but that also let the plane rotate infinite this is the whole code if that helps:

 void Update() {
 
 
         if (Input.Get$$anonymous$$ouseButton(0)) {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             
             if (Physics.Raycast(ray, out hit, 100)) {
                 // whatever tag you are looking for on your game object
                 if(hit.collider.tag == tag) { 
                     maze.transform.rotation *= Quaternion.Euler(yrotation*Time.deltaTime,0,xrotation*Time.deltaTime);
 
                     Debug.Log("---> Hit: ");
                     
                 }
             }
             
             
         }
avatar image mr_duke · Dec 10, 2015 at 07:23 PM 0
Share
 private const float STOP_ANGLE = 20;
 private const float SPEED_ANGLE = 180;//per second
 public  GameObject maze;
 
 private float mazeYangle = 360;//curren angle
 RaycastHit hit;
 
 private void Update()
 {
     if ( Input.Get$$anonymous$$ouseButton( 0 ) && Physics.Raycast( Camera.main.ScreenPointToRay( Input.mousePosition ), out hit, 100 ) )
     {
         var oldAngle = mazeYangle;
         mazeYangle = ( mazeYangle + SPEED_ANGLE * Time.deltaTime - STOP_ANGLE ) % 360 + STOP_ANGLE;
         maze.transform.Rotate( 0, mazeYangle - oldAngle , 0 ) ;
     } else
     {
         var oldAngle = mazeYangle;
         mazeYangle = $$anonymous$$athf.$$anonymous$$in( mazeYangle + SPEED_ANGLE * Time.deltaTime - STOP_ANGLE, 360 ) + STOP_ANGLE;
         maze.transform.Rotate( 0, mazeYangle - oldAngle, 0  );
     }
 }

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

33 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

Related Questions

Rotating Vector3, linecast 1 Answer

unwanted rotation in y axis 0 Answers

How can I set rotation properly? 0 Answers

Rotation problem 0 Answers

transform rotation inaccuracy 0 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