• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Draw a ray from a gamobject and keep direction of the ray relative to the gameobjects rotation. 1 Answer

Space Shooter Tutorial tilting extra 0 Answers

How to set a max rotation angle 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges