• 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 unity_oyq4fQ2B_8I89Q · Mar 05, 2019 at 04:35 PM · for-loopquaternionsrotations

Dividing rotation into segments of 45 degrees

Hi there. I'm making t$$anonymous$$s little game that I need t$$anonymous$$s mechanic for. I want characters to rotate in 45 degree intervals so there's 8 total directions a player can be facing and firing. I had a system where they could just turn 360 degrees but that won't work once I'm going to add visuals. I've been puzzling with it for a few days now and I can't t$$anonymous$$nk of a decent solution. Sure, I could write somet$$anonymous$$ng like

//Constantly updating rotation here if (rotation => -22.5 && rotation < 22.5) { rotation = 0; }

but I know it can be more efficient than that, I just can't figure out how. I'm guessing there's probably a way to regulate t$$anonymous$$s using a for statement and an array with all the rotations, but I just can't figure it out. Any help would be greatly appreciated.

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 mchts · Mar 05, 2019 at 07:30 PM

Not sure what you want exactly but lets say you want to rotate your character by 45 degrees around its center depending on finger or mouse position. I've posted a simple script here. Just attach t$$anonymous$$s script to your sprite see it in action. You could simply change the direction with replacing Vector3.forward to whatever direction you want to rotate (i assume it must be x). And once and for all unity 2D is actually 3D with no depth (like scale z is always 0). So there is no problem using Vector3 in 2D platform.

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 unity_oyq4fQ2B_8I89Q · Mar 06, 2019 at 09:24 AM 0
Share

This worked really well, thanks :D

avatar image
0

Answer by WarmedxMints · Mar 05, 2019 at 04:42 PM

I've recently messed with t$$anonymous$$s in an open source project. You can check out the project here - Git Hub


In the gun behaviour script, you will find t$$anonymous$$s;

                     //Get current rotation
                     var newRot = transform.rotation.eulerAngles;
                     //Snap it to the nearest desired degree on each axis
                     newRot.x = Mathf.Round(newRot.x / SnapRotationDegrees) * SnapRotationDegrees;
                     newRot.y = Mathf.Round(newRot.y / SnapRotationDegrees) * SnapRotationDegrees;
                     newRot.z = Mathf.Round(newRot.z / SnapRotationDegrees) * SnapRotationDegrees;
                     //Convert it from a Vector3 back into a Qauternion
                     var rot = Quaternion.Euler(newRot);
 
                    //Do what we want with the new rotation
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 unity_oyq4fQ2B_8I89Q · Mar 05, 2019 at 05:55 PM 0
Share

I've been reading through the code but I am having a bit of trouble comprehending everything. But that Mathf.Round function will probably come in handy since it fixes one of the problems I had with an earlier idea. I'ma see if that's gonna work. Also, I forgot to mention it's a 2D game a bit like surviv.io, which might also be why I'm not understanding everything (I've only really done 2D games and I'm really new regardless)

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

103 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotations, Quaternions and rigidbody physics 3 Answers

Can I use Quaternions to get from one point to another? 1 Answer

Quaternions rotating left and right, not forward and back 1 Answer

4 rotations WTF 1 Answer

Relative Rotations and Position calculation 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