Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 Nephtis · Sep 22, 2018 at 04:52 PM · 2dcollisionvector2collision2dcoordinates

Find cardinal direction of collision based on 2D Cartesian coordinates

Basically I want to find the angle/direction/whatever at the point where an object hits a wall in a 2D Cartesian space (x y). I'd like to be able to map this to one of the four cardinal directions: Up, Down, Left Right. My object does not rotate, it's just a sprite that plays a different animation depending on key presses. alt text

The problem is that, while I have Vector2s for the object and the wall, the wall (i.e. its collider) is quite long, so just comparing the transform.position of each object won't really work, because the wall's "position" (I assume the centre of its area) could be somewhere far away from the impact point.

Then I found out about collision.contacts[0].point, which apparently gives the point of contact between two colliders. So I'm currently using that in the form of:

 Vector2 dir = collision.contacts[0].point - (Vector2)transform.position;
 float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;



And then trying to hard-code the angle to correspond to one of the four directions, e.g. angle > 145 = Left.

But this doesn't work how I expect all the time. For example, sometimes although the object hits a wall to its left, giving an angle of 130, this makes it think it's hit a wall above it. Playing with the ranges doesn't really help because I keep finding cases where it doesn't quite work. I did also try Vector2.Angle, but this produces basically the same problem, and also doesn't give me the results I expect in the first place (probably because I've misunderstood how it works).

So I think I must be missing something regarding my initial approach. Is there a better way to do this?

wall-hits.png (6.7 kB)
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

1 Reply

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

Answer by LCStark · Sep 22, 2018 at 05:13 PM

Regarding the Vector2.Angle - I think you want to use Vector2.SignedAngle instead. Angle would give you +90 degree angle for both left and right.

Comment
Add comment · Show 7 · 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 Nephtis · Sep 22, 2018 at 05:22 PM 0
Share

So upon doing:

 Vector2 dir = collision.contacts[0].point - (Vector2)transform.position;
 float signed = Vector2.SignedAngle(dir, (Vector2)transform.position);

I can map the cardinal direction as such: Left = (-180 to -90), Up = (-90 to 0), Right = (0 to 90), Down = (90 to 180). SignedAngle seemed to do the trick, no issues so far. Thanks a lot!

avatar image LCStark Nephtis · Sep 22, 2018 at 05:31 PM 0
Share

Ah, there's your problem. You're getting the SignedAngle between dir and position, which is the wrong thing to do. You want to compare the direction with some reference vector (e. g. Vector2.up).

float signed = Vector2.SignedAngle(dir, Vector2.up)

avatar image Nephtis LCStark · Sep 22, 2018 at 05:34 PM 0
Share

I don't think I can use Vector2.up (or forward, or whatever) because my object doesn't rotate, so it'll always have the same "up". Anyway, SignedAngle seems to be working ok, I don't know if it's perfect but it works a lot better than what I had before.

EDIT: Ok, so there is a problem - I got hit to the left, and wanted the angle to be in the (-180, -90) range, but I got +145. It happened when I clipped the very bottom right corner of a collider. SO maybe you are right and I need to have some sort of notion of "forward" - I might have to do this manually since my sprite doesn't rotate. I think I can use $$anonymous$$oveDirection, if I just keep track of this when a force is applied and compare against it.

Show more comments

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

223 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 to get click position in Vector 2 ? 1 Answer

Tilemap collider doesn't appear 1 Answer

Check if a Collider2D is hit by a Trigger Collider2D 1 Answer

Box Collider 2d Not Colliding 0 Answers

How to move objects away from each other (not what you think) 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