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 Batka · Feb 13, 2017 at 08:20 AM · physics2dcollider2d

ContactPoint2D.contacts[0].normal returns sometimes returns different value that breaks my game.

Hi,

I have a 2d game where the ball (Player) bounces from obstacles like a laser using Vector2.Reflect.

I tried everything, and through the debugger I noticed that sometimes ContactPoint2D.contacts[0].normal returns a different value.

So, with this function, I reflect the ball:

 void ReflectBall(ContactPoint2D contact, Vector2 ballDirection)
         {
             Vector2 inDirection = Vector2.Reflect(ballDirection, contact.normal);
             float rot = Mathf.Atan2(inDirection.y, inDirection.x) * Mathf.Rad2Deg;
             _transform.eulerAngles = new Vector3(0, 0, rot);
 
         }

And now, using the debugger, I see that the normal of the contact point is x(0.5) y(-0.9)

alt text

Now I set the debugger to break only when the normal does not equal that value (0.5, -0.9). And I tested on the same game object, hitting it on the same spot:

alt text

Here is the object with it's collider:

http://prntscr.com/e7x0dj

as well its physics components:

http://prntscr.com/e7x0nd

Now here is the OnCollisionEnter function:

  void OnCollisionEnter2D(Collision2D col)
         {
             Vector2 forward = _transform.right;
             // ball hit bound of gamefield, level is going to reset
             if (col.gameObject.layer == LayerMask.NameToLayer(Constants.LayerBounds))
             {
                 shouldResetTransform = true;
                 Reset(false);
             }
 
             //if ball hit on figure
             else
             {                
                 string tag = col.gameObject.tag;
                 if (tag.Equals("Normal") || tag.Equals("Hard") || tag.Equals("NotDisappear"))
                 {
                     ContactPoint2D contact = col.contacts[0];
                     ReflectBall(contact, forward);
                     OnHitResponse(col.gameObject);
                 }
 
             }
         }

OnHitResponse() is a heavy function, but it's called after RefflectBall().

I'd really appreciate any ideas. Thanks :)

screenshot-2.png (24.3 kB)
screenshot-1.png (24.9 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
2

Answer by MelvMay · Feb 13, 2017 at 09:40 AM

For starters, something is very odd with that normal as it's supposed to be normalized and (0.5, -0.9) clearly isn't.

Ensure that you're using continuous collision detection otherwise you can be penetrating the other collider.

Also, changing the Transform of a GameObject that uses a Dynamic body-type Rigidbody2D is bad behaviour. If you want to modify the rotation of a Rigidbody2D then set its rotation property i.e. let the Rigidbody2D type drive the Transform, not the other way around.

Comment
Add comment · Show 1 · 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 Batka · Feb 14, 2017 at 10:38 AM 0
Share

As far as I understand, this is a surface normal, not a normalized vector.

A surface normal vector is the vector which angle with the surface is perpendicular.

See this

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Physics2D.IgnoreCollision not working! 0 Answers

Collider2D.GetContacts() Checking One Physics Step Behind 0 Answers

How to use OverlapCollider? 1 Answer

Box colliders laid side by side are uneven and causes bumps when the player moves from one collider to another. 1 Answer

Physics2D.IgnoreCollision Not Working 0 Answers


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