Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 photophone · May 28 at 01:08 AM · collision detection

why is oncollisionenter not working half the time?

I am making DX ball.

for the movement of the ball, it would be too hard to use a rotation to make a reflection. so instead, I made 2 tags, border X and Y, and 2 vars for the speed of the ball on the x and y. I added these tags on the borders of the screen, and on 2 colliders for each brick. the problem is, when the ball hits the brick, the ball reacts to the collision, but dosent get an oncollisionenter() call for some reason and the brick dosent get destroyed and the ball dosent reflect.

here's my code for the ball movement:

 public Rigidbody2D rb;
 public float movespeed = 1f;
 private float speedX;
 private float speedY;

 void FixedUpdate()
 {
     transform.position = new Vector3(transform.position.x + (speedX * (movespeed/10)), transform.position.y + (speedY * (movespeed/10)), 0);
 }

 void OnCollisionEnter2D(Collision2D other)
 {
     if (other.gameObject.tag == "borderX")
     {
         speedX *= -1f;
         Debug.Log("border X");
     }

     if (other.gameObject.tag == "borderY")
     {
         speedY *= -1f;
         Debug.Log("border Y");
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Yuki_Rain · May 30 at 06:14 PM

do not set position while you are using rigidbody, use rigidybody.move to move your gameobject.

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 tushardholakiya · May 28 at 06:06 AM

Try to add debug outside if condition to check it's invoking OnCollosionEnter2D when it actually enters or not

 void OnCollisionEnter2D(Collision2D other)
  {
      Debug.Log(other.gameobject.name);//------ Ad this line

      if (other.gameObject.tag == "borderX")
      {
          speedX *= -1f;
          Debug.Log("border X");
      }
      if (other.gameObject.tag == "borderY")
      {
          speedY *= -1f;
          Debug.Log("border Y");
      }
  }

can you inspector video pic of ball object(where this script is attached)

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 photophone · May 28 at 04:05 PM 0
Share

thank you for the response!

I tried adding the debug.log statement outside the if statement, and it turns out it's not doing oncollisionenter2D() at all. for the picture of the ball, I added a picture of the ball's inspector below.

unity-ball-inspector.png (60.8 kB)

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

142 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

Related Questions

use OnParticleCollision(GameObject)for AOE spell/magic creation problem 1 Answer

How to detect different parts in sprite? 2 Answers

How do I check that no collisions have a tag? 1 Answer

Help with collision detection with a prefab on a gamemanager 0 Answers

Raycast goes through collider 2 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