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
0
Question by CliffracerX · Aug 13, 2014 at 07:18 PM · rotationconstraint

How to limit object's Y rotation to 45 degree increments

I'm looking to make buildable walls in my game, which I would like to be able to rotate based on the direction the player is looking. However, I want to limit it's Y rotation to 45 degree increments (0, 45, 90, etc), however so far my attempts result in the rotation on the Y axis being stuck at 0. Here's my code, if it's any help:

 tempRot.y=Mathf.Round(tempRot.y/45)*45;

Any ideas or suggestions on a different way to do this?

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 christoph_r · Aug 13, 2014 at 07:24 PM 0
Share

What's tempRot and where is that piece of code called? If it is supposed to be the rotation resulting from a mousedrag, keep in $$anonymous$$d that by rounding you are ALWAYS rounding it to 0, unless the movement of the mouse is very quick. I'm assu$$anonymous$$g you're having some sort of display of where the wall will be placed and at what angle, right? In that case you'll most likely want a separate variable for keeping track of the input rotation and the rotation of your 'gizmo'. Your other lines for tempRot can probably stay the same, but the rotation of that gizmo should actually be the code you posted here.

avatar image CliffracerX · Aug 13, 2014 at 07:51 PM 0
Share

tempRot is a Quaternion that starts off equal to the camera's transform.rotation variable. I change the x/z to 0 (so you can't make an upside wall, for instance), and change the y in the code seen above.

Here's the whole bit of code, it's part of FixedUpdate():\

 Quaternion tempRot = camera.transform.rotation;
         tempRot.x=0;
         tempRot.z=0;
         tempRot.y=$$anonymous$$athf.Round(tempRot.y/45)*45;
         int distance=3;
         if(buildType==1)
         {
             Vector3 newpos = camera.transform.position + camera.transform.forward*distance;
             firstBuildablePrefabBuildPos.transform.rotation=tempRot;
             Ray ray = new Ray(newpos, Vector3.down);
             RaycastHit hit;
             Physics.Raycast(ray, out hit, 2.5f);
             
             {
                 firstBuildablePrefabBuildPos.transform.position = new Vector3($$anonymous$$athf.RoundToInt(newpos.x/2)*2, hit.point.y+1, $$anonymous$$athf.RoundToInt(newpos.z/2)*2);
             }
         }

firstBuildablePrefabBuildPos (long name, I know) is the preview gizmo.

2 Replies

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

Answer by robertbu · Aug 13, 2014 at 08:19 PM

The individual components of a Quaternion (x, y, z, w) are not angles. They are values between -1 and 1. For the description of how you have things setup, you can use tempRot.eulerAngles.y instead:

  Vector3 vv = tempRot.eulerAngles;
  vv.y = Mathf.Round(vv.y/45.0f) * 45.0f;
  tempRot.eulerAngles = vv;

As long as you are only rotating on the 'y', then you will be okay, but for more general rotations, it is 'dangerous' to read from eulerAngles. The representation, while accurate, can change. For example (180,0,0) might become (0,180,180)...the same physical rotation represented differently.

Edit: How you are setting up tempRot is also an issue. Do this instead:

  Vectror3 v = camera.transform.forward;
  v.y = 0.0f;
  Quaternion tempRot = Quaternion.LookRotation(v);
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 CliffracerX · Aug 13, 2014 at 08:36 PM 0
Share

That fixed it! Thanks!

avatar image
0

Answer by IbQuezada · Aug 13, 2014 at 09:03 PM

Try using modulus, e.g.

 float angle = 90;
 if (angle % 45 == 0) { // if the result of angle divided by 45 is 0
     // do the rotation
 }




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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unlock rotation from script 1 Answer

Flip over an object (smooth transition) 3 Answers

Manually computing rotation constrain limits 0 Answers

Unity 2d - Rigidbody rotation constraints? 4 Answers

Rigidbody constraints in local space? 7 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