• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

140 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

Related Questions

Calculating the Angle between two vectors 2 Answers

How to make two objects collide without bouncing 2D 0 Answers

2D Custom Bouncing Diagonal 0 Answers

Move Object With Angle In 2D 1 Answer

How to move objects away from each other (not what you think) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges