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 RexGrey · Mar 13, 2012 at 06:09 PM · rotationguiclamp

Clamping Rotation

Hi everybody! I want to create specific tranformation of the camera: when I press GUI buttons, the camera moves up/down (some kind of a zoom) and at the same time it is rotated from X=90 to X=50 and back again. The problem is that I can't clamp this range of rotation properly. Here is what I have:

 function Update()
 {
     transform.localPosition = Vector3( 0, Mathf.Clamp(transform.localPosition.y, MinY, MaxY), Mathf.Clamp(transform.localPosition.z, MinZ, MaxZ));
     transform.eulerAngles = Vector3(Mathf.Clamp(transform.eulerAngles.x, 50.0, 90.0), 0, 0);
 }
 function OnGUI()
 {
     var movement = new Vector3( 0, -60, 27);
     if (GUI.RepeatButton(Rect(ButZIposX,ButZIposY,ButZIwidth,ButZIheight), "Zoom In"))
     {
       transform.Translate(movement * Speed * Time.deltaTime);
       transform.Rotate(Vector3(-1,0,0) * Time.deltaTime);
     }
     if (GUI.RepeatButton(Rect(ButZOposX,ButZOposY,ButZOwidth,ButZOheight), "Zoom Out"))
     {
       transform.Translate(movement * -Speed * Time.deltaTime);
       transform.Rotate(Vector3.right * Time.deltaTime);
     }
 }

Is there any suggestions?

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
0
Best Answer

Answer by RexGrey · Mar 16, 2012 at 05:36 PM

It is as easy as a Goo pie))

 if (GUI.RepeatButton(Rect(ButZIposX,ButZIposY,ButZIwidth,ButZIheight), "Zoom In"))
     {
         
         if (transform.eulerAngles.x > 50.0)
         transform.Rotate(-Vector3.right * Time.deltaTime);
     }
     
      if (GUI.RepeatButton(Rect(ButZOposX,ButZOposY,ButZOwidth,ButZOheight), "Zoom Out"))
     {
         
         if (transform.eulerAngles.x < 86.0)
         transform.Rotate(Vector3.right * Time.deltaTime);
     }    

The problem is that Unity reacts incorrectly on values of rotation: after reaching a value of about 87, it shows exactly 90, so when I wrote "transform.eulerAngles.x < 90.0" it stopped rotating without reaching required value

Anyway, thanks for help

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 Berenger · Mar 13, 2012 at 06:27 PM

You should not rotate and translate in OnGUI, as it is called several times per frame. Besides that, the code seems ok. You can try something different, a lerp between the current rotation and a rotation of 50 (or 90 if going the other way).

PS : -Vector3.right == Vector3(-1,0,0)

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 RexGrey · Mar 16, 2012 at 05:01 PM 0
Share

Well, I tried $$anonymous$$athf.LerpAngle, and what I have now is that:

 function Update() 
 {
      if(zoo$$anonymous$$)
      {
         var anglein : float = $$anonymous$$athf.LerpAngle(90.0, 50.0, Time.time * 0.05);
         transform.eulerAngles = Vector3(anglein, 0, 0);
      }
     
      if(zoomout)
      {
         var angleout : float = $$anonymous$$athf.LerpAngle(50.0, 90.0, Time.time * 0.05);
         transform.eulerAngles = Vector3(angleout, 0, 0);
      }
 }
 
 function OnGUI()
 {
         if (GUI.RepeatButton(Rect(ButZIposX,ButZIposY,ButZIwidth,ButZIheight), "Zoom In"))
         zoo$$anonymous$$ = true;
 
     if (GUI.RepeatButton(Rect(ButZOposX,ButZOposY,ButZOwidth,ButZOheight), "Zoom Out"))
         zoomout = true;    
 }

Of course, it doesn't work either. Firstly, it continues to change rotation when I'm not pressing the button. Secondly, rotation starts with a small leap, but then it moves gradually. Thirdly, Time.time is not appropriate for my case.

$$anonymous$$ay there be other suggestions?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera global rotation clamping issue 0 Answers

3D top down rotation 1 Answer

Restricting GameObject Rotation 1 Answer

C# Clamp joystick rotation 0 Answers

GUI Rotation and Depth problem 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