Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Bongmo · May 02, 2017 at 08:30 PM · rotationgameobjecteuleranglesclampdegrees

Limit gameobject rotation to -480 and 480 degrees?

I can't clamp the game object rotation to -480 and 480 degrees?

This is the video for my problem: Touch rotate object

You can find the code here: Touch rotate object code

I've tried to to add the transform.localEulerAngles.z to a variable, but the angle is wrong.

  testAngle += transform.localEulerAngles.z;

The problem here is, that I only get numbers from 0 - 360 from the game object. I must somehow calculate the angles or can I somehow get transform rotation greater 360?

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 FortisVenaliter · May 02, 2017 at 08:44 PM 0
Share

Not sure I understand... Greater than 360 loops around to 0 again. That's just how degrees work. In terms of orientation, there is zero difference between angles 10 and 370. You'd have to track and store the rotation yourself to put those limits on it.

avatar image Bongmo FortisVenaliter · May 02, 2017 at 09:17 PM 0
Share

And this my problem. I don't know how to do that? $$anonymous$$aybe I searched the wrong keywords on google. Can you please give some links or tips if you have some.

2 Replies

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

Answer by Eno-Khaon · May 03, 2017 at 01:05 AM

It's slightly more involved to handle larger angles, but not *THAT* bad. Rather than worrying about absolute angles, you can instead handle relative angles.

Using the linked script as a template, I made a few adjustments:

 using UnityEngine;
 
 public class ClickDragRotate : MonoBehaviour
 {
     public float minAngle = -480;
     public float maxAngle = 480;
 
     float angle = 0;
     float lastAngle = 0;
 
     void OnMouseDown()
     {
         Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
         pos = Input.mousePosition - pos;
         lastAngle = Mathf.Atan2(pos.y, pos.x);
     }
 
     void OnMouseDrag()
     {
         Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
         pos = Input.mousePosition - pos;
         // 2D rotation matrix to undo the current rotation
         // [cos(T) -sin(T)]
         // [sin(T)  cos(T)]
         Vector3 rotatedPos = new Vector3(pos.x * Mathf.Cos(-lastAngle) - pos.y * Mathf.Sin(-lastAngle), pos.x * Mathf.Sin(-lastAngle) + pos.y * Mathf.Cos(-lastAngle), 0);
         float currentAngle = Mathf.Atan2(rotatedPos.y, rotatedPos.x);
         angle += currentAngle * Mathf.Rad2Deg;
         lastAngle = Mathf.Atan2(pos.y, pos.x);
         angle = Mathf.Clamp(angle, minAngle, maxAngle);
         transform.rotation = Quaternion.AngleAxis(angle, transform.forward);
     }
 }

I save the last frame's angle, then use that to rotate the current frame's position vector backward. At that point, Atan2() can be used safely without (usually) worrying about the -180/180 degree boundary. Once I have that new angle for the current frame, I simply add that to the current total angle and clamp it between the defined minimum and maximum angles.

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 toddisarockstar · May 03, 2017 at 01:10 AM

in Eularangles, Angles are represented between zero and 360. In other words, if there where an angle of 480 it would be the same as 120. It's just like a clock's hand passing around in cirlces past 12.

you can use Quaterntions to represent angles also. and they will except numbers that are higher than 360 to save "loop around" info but they are more complicated to learn than eular angles. I reccomend you stick to eular angles though.

if you give a little more info on what you are trying to do We can pry help you fix your problem.

anyways, you can always store rotations above 360 like this if you really need to know for whatever reason

         transform.eulerAngles = Vector3.zero;
         int myspins=0;
         
         float addtoangle=480;
            addtoangle=addtoangle+transform.eulerAngles.z;

         while (addtoangle>360f) {myspins++;
                                  addtoangle += -360f;
                                  }
 
         transform.eulerAngles += new Vector3 (0, 0, addtoangle);
 
         float f = myspins * 360f;
         f = f + transform.eulerAngles.z;
 
         print ("the rotation you are asking for is : " + f);
         print ("but the real  rotation your  pointing is : " + transform.eulerAngles.z);
         print ("i spun around " + myspins + " times get here. as long as i know this i can always convert ");



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

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

96 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

Related Questions

transform.eulerAngles Problem 0 Answers

Clamp Rotation Problem 1 Answer

Rotation not observing limits 1 Answer

Limit Rotation of physics object in 2D 1 Answer

Bizzare problem with limiting camera veritcal rotation 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