• 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
3
Question by hurleybird · Jun 16, 2014 at 08:41 PM · 2dangle

How to get the angle between two objects with OnTriggerEnter2D

Hi, I'm making a 2D top down space shooter and I'm trying to get the angle between the projectiles that my ships fire and the object (which is a trigger) that they enter. This is because in my game I have shield facings and need to determine which facing to deduct hit points from.

I'm trying to use this:

 void OnTriggerEnter2D(Collider2D other) {
     Vector3 targetDir = other.transform.position - gameObject.transform.position;
     Vector3 forward = transform.up;
     float angle = Vector3.Angle(targetDir, forward);
     Debug.Log (angle);
     }

But the numbers I'm seeing are all over map and not at all usable. Even if the numbers were good, this method seems to give no way to distinguish between the left and right facings (eg. if it actually worked, getting hit on either side would return something around positive 90 degrees)

This would likely be a simple problem if I were to use rigid bodies, but I am not using rigid bodies and am not able to. Instantiating the projectiles as rigid bodies seems to a cause a giant host of issues, including (though not limited to) a massive amount of unsightly "judder" or "hitching".

Any help would be appreciated, thanks!

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 hurleybird · Jun 17, 2014 at 01:26 AM 0
Share

Okay, so I have this more or less figured out. Robertbu was more or less right, but was missing one little piece. "transform.InverseTransformDirection(dir);" needs to be "other.transform.InverseTransformDirection(dir);"

The correct code is as follows:

  Vector3 dir = other.transform.position - transform.position;
  dir = other.transform.InverseTransformDirection(dir);
  float angle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;

Robert, thanks a bunch. If you edit your solution to the above I'll accept it as correct.

avatar image robertbu · Jun 17, 2014 at 04:15 AM 0
Share

Edited. I guess I did not understand which object needed the relative angle.

3 Replies

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

Answer by robertbu · Jun 16, 2014 at 09:25 PM

Given this is 2D, Atan2 should work. Using Transform.InverseTransformDirection() will make the angle relative to the game object rather than a world direction:

    Vector3 dir = other.transform.position - transform.position;
    dir = other.transform.InverseTransformDirection(dir);
    float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;

I believe the angle you get back will be -180 to 180 based on Vector3.right as the 0.0 angle. I see you are using transform.up as your current forward, so you can either normalize the result to using up, or you rotate your sprite so that forward is 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 hurleybird · Jun 16, 2014 at 10:23 PM 0
Share

This seems to be closer to what I'm looking for but the exact implementation you've set given seems to have a few issues. It does return from -180 to 180, but there are two points on a 2D circle collider where it will return each value. For example, without normalization firing on either the leftmost or rightmost side of the collider returns -90, front and back return 90, 0 occupies two of the diagonals with 180 for the other two.

avatar image robertbu · Jun 16, 2014 at 10:25 PM 0
Share

Did you catch my edit to this code. It needs to be InverseTransformDirection() not TransformDirection().

avatar image hurleybird · Jun 16, 2014 at 10:45 PM 0
Share

I have now that you've pointed it out, but the modification seems to make things worse. The numbers returned now seem like junk. Never negative, and nothing ever close to 0. Looks like between 40 and 150 and the only pattern I can see is that directly in front, behind, left, and right all return 90.

avatar image Pyrian · Jun 16, 2014 at 11:04 PM 0
Share

Atan2 is pretty reliable, but garbage in, garbage out. I would use debug to take a hard look at the values getting passed in (dir.y, dir.x). There's no reason why opposite angles should result in the same output - but there's plenty of ways they can end up looking that way.

avatar image hurleybird · Jun 16, 2014 at 11:15 PM 0
Share

Well, the only two inputs are a simple circle collider for the object and a box collider for the projectile. The only manipulation being done on either is for the projectile to move forward via transform.Translate. I don't see any place for 'garbage' to get in the way, so I'm thinking the problem is somewhere in these two lines:

  Vector3 dir = other.transform.position - transform.position;
    dir = transform.InverseTransformDirection(dir);
Show more comments
avatar image
1

Answer by allformegrog · Jul 31, 2017 at 05:13 AM

As of version 2017.1 there is a Vector3.SignedAngle function that looks like it will do as you need.

https://docs.unity3d.com/ScriptReference/Vector3.SignedAngle.html

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

Answer by thomasindustry · Jun 16, 2014 at 09:24 PM

The Mathf.Atan2 function might give you better angles

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.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity2D move + turn with a fixed angle 1 Answer

How to get rotation for projectile to fire at cursor? 1 Answer

2D Animation does not start 1 Answer

2D Angle Calculation 0 Answers

Calculate the normal of a collider 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