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
0
Question by watkinza · Nov 03, 2015 at 04:54 PM · movementscripting beginner2d rotation

Converting 2D square movement direction to procedural animation

Hello, and thanks for checking out my question!

I am trying to figure out a good way to animate a 2D square's rotation based on which direction the player is moving, so that the face of the square closest to this direction is rotated towards the point.

The square is the player, has 2D physics with a collider, and although I would like it to be accurate down to 1 degree I am fine with covering the major and minor cardinal directions.

One thought I had was to create a circle sliced into 8 pieces and raycasting it to figure out which slice the vector falls within, but I don't even know if I can achieve this in a performant way. Example:

alt text

I searched the Asset Store for a 2d character controller but was unable to find one that advertised this behavior. The original approach was to convert the direction to a degree around the center of the square in comparison to the zero-degree side of the square, and then try to figure out which side to rotate towards the point, but my attempts using Vector2.Angle() have been unsuccessful.

Thanks again for your time! This is my first question here, so please let me know if I need to provide more information.

example-raycast-square-coords.jpg (35.4 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 Statement · Nov 03, 2015 at 05:11 PM 0
Share

This question is pretty similar to this other question that I provided an answer for. Although, it uses 4 sectors ins$$anonymous$$d of 8. It should be pretty simple to modify the Quantize method to support 8 sectors. I am sure it's possible to quantize the angle without having to do the ugly if tests too, but uh, I left that as an exercise :)

You can also play a webgl demo here to get an idea if it's somewhat you're looking for.

avatar image watkinza Statement · Nov 03, 2015 at 06:45 PM 0
Share

Wow, ok that may be what I was looking for. I've downloaded the package and am digging through it now. Thanks for your help!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Statement · Nov 03, 2015 at 07:24 PM

Well, if you get an angle to somewhere, like the mouse cursor, you can quantize the angle to 8 sectors.

 public static float QuantizedRadian(float radian, int sectors = 8)
 {
     float unit = radian / (Mathf.PI * 2);
     int sector = Mathf.FloorToInt(unit * sectors);
     return sector * Mathf.PI * 2 / sectors;
 }

That will give you a radian that snaps to 8 different angles etc. But your example also was offset by half a sector in radians so you need to take that into account. Anyway, if you want a more complete example, I wrote a demo. The demo takes angle to mouse, offsets & quantizes the angle. Additionally, it shows which sector the angle points to.

  • Binaries: WebGL Demo, Unity Package

  • Code: Quantize.cs, QuantizedExample.cs

  • Project: Github

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

32 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

Related Questions

How do I make my movement script move the character UNTIL I let go of the key? 1 Answer

Help me to do this moving player script 0 Answers

Script problems c# 0 Answers

Rigidbody2D movement is lagging 0 Answers

How do I make an object move in the direction its facing? 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