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 /
avatar image
0
Question by MikeErty · Dec 18, 2017 at 10:25 AM · 2danglecollision2dbouncereflect

How to calculate angle between 2D objects and use the result?

Firstly I'm not new to Unity but I do struggle to learn code without having some clear examples to work from. I'm having a hard time deciphering how to use the code in the docs because they're not used in a context that I understand.

That being said, my problem is quite simple and I'm sue the answer is in the docs for contactpoints2d etc.

I'm firing a 2d object (a circle - bullet), at enemies (a collection of 2d boxes).

I want to detect the angle of impact from the bullet and then work out if the angle was too shallow for a penetration. If it was, the bullet simply bounces off. Just like in Company of Heroes.

I tried two vesions of the same code and each has it's own weirdness.

1 -

     void OnCollisionEnter2D(Collision2D other){
         Vector2 normal = other.contacts [0].normal;
         Debug.Log(Vector2.Angle (other.gameObject.transform.position, -normal));
         if (Vector2.Angle (other.gameObject.transform.position, -normal) < _maxAngle) {
             //bullet bounces off
             Debug.Log("bounce off");
         } else {
             //bullet penetrates
             Debug.Log("Penetrates");
             TakeDamage (other);
         }
     }

In this version the angle reported in the debug seems to be around 176 no matter where I shoot from, UNTIL I shoot from a very oblique angle - at which point it changes to around 70.

 void OnCollisionEnter2D(Collision2D other){
         Vector3 normal = other.contacts [0].normal;
         Debug.Log(Vector3.Angle (other.gameObject.transform.position, -normal));
         if (Vector3.Angle (other.gameObject.transform.position, -normal) < _maxAngle) {
             //bullet bounces off
             Debug.Log("bounce off");
         } else {
             //bullet penetrates
             Debug.Log("Penetrates");
             TakeDamage (other);
         }
     }

This is the original version I was using. The result of this one way promising when firing from the bottom of the screen upwards at a box2d. The angles seemed to be quite sensible and it seemeed to be mostly there. However, when I shot sideways at another box the angles were completely different to what I'd expect and it was useless.

It's worth noting that I was using 4 boxes as armour, arranged in a square, to represent the sides of a vehicle. They were child objects of a parent "enemy". They are just scaled boxes - maybe scaling them affects normal detection? I have also tried with the boxes pulled out to the root of the scene and it seemed to make no difference.

Thanks!

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

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

139 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

Related Questions

2D Reflect(bounce) With Rotation 0 Answers

Finding the angle between 2 clicked points 2 Answers

PlayOneShot not playing sound 1 Answer

2D GunPivot Bug 1 Answer

Ball bounce problem 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