Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by ThomasSoto · May 06, 2019 at 09:38 PM · vector3directionvector2snapsnapping

Snap direction to angles + shifting

I currently have this following code which snaps a v3 direction to the specified angles

     Vector3 SnapToAngle(Vector3 vector3, float snapAngle, float snapAngleShift) {
         float angle = Vector3.Angle (vector3, Vector3.up);
         if (angle < snapAngle / 2)                        // Cannot do cross product 
             return Vector3.up * vector3.magnitude;        // with angles 0 & 180
         if (angle > 180 - snapAngle / 2)
             return Vector3.down * vector3.magnitude;
 
         float t = Mathf.Round (angle / snapAngle);
         float deltaAngle = (t * snapAngle) - angle;
 
         Vector3 axis = Vector3.Cross(Vector3.up, vector3);
         Quaternion q = Quaternion.AngleAxis (deltaAngle, axis);
         return q * vector3;
     }

This was provided by @robertbu in this post. https://answers.unity.com/questions/493006/snap-a-direction-vector.html

Now. What I want to do, is to be able to shift the snap angle in such a way that if I set it to snap on 180 degrees increments, I can rotate the axis in which it snaps. Take a look at the image below.

alt text

any suggestions on how I can proceed?

illustrator-2019-05-06-17-36-10.png (41.6 kB)
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 Bunny83 · May 07, 2019 at 01:08 AM 0
Share

Sorry but it's not quite clear around which axis you actually want to rotate. The original question where the code came from wanted to snap the rotation around the local x axis in 3d space. You drawn a pure 2d diagram with no references. The code of robert allows free rotation around the y axis but snaps the rotation around the local x axis.


Generally to apply a shift you just want to apply your shift rotation before any of the original code and undo that rotation before you return the result. However since the axis of rotation is not fixed in this code we can not apply a relative rotation beforehand since we don't know the axis yet. So if you want for example a 10° shift but the vector point exactly up it's impossible to deter$$anonymous$$e a rotation axis for your shift. In his code he has two special cases for up and down. Though this only works because up and down are actual target snap angles.

avatar image ThomasSoto Bunny83 · May 07, 2019 at 01:06 PM 0
Share

Sorry for not being clear enough. The game I'm making is a 2d side scroller platformer that involves shooting. Therefore the rotation and snapping involve the Z axis. I've tried several approaches but I'm not really the best at math. Am I making my self clear?

0 Replies

· Add your reply
  • Sort: 

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

197 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 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

Unity Snap settings. moving slightlty wrong 1 Answer

NullReferenceException: Object reference not set to an instance of an object 2 Answers

ClampMagnitude while ignoring a direction 0 Answers

How to get a vector 3 to vector 2 - c# 1 Answer

C# How can create a list of floats from different types of structs? 0 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