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 /
  • Help Room /
avatar image
0
Question by Skeletor101 · Aug 07, 2016 at 11:09 PM · unity 5collision2d

OnCollisionEnter2D not working

My game is a simple brick break game. The code currently is supposed to work in this sequence. The Ball is fired from the Paddle and hits a Brick. When this collision happens it should activate my level manager script to change to the next level.

My problem is that the collision between the brick and the ball is not registering. None of the print statements appear in the console when the ball hits the brick, HOW EVER THE BALL DOES BOUNCE OFF THE BRICK.

Any help would be appreciated.

Your evil overlord,

Skeletor

Here is the Inspector information of the Ball alt text

And here is the Balls associated code

 using UnityEngine;
 using System.Collections;
 
 public class Ball : MonoBehaviour {
 
 
     private  Paddle paddle;
     public Rigidbody2D body;
     private bool hasStarted = false;
     private Vector3 paddleToBallVector;
 
     // Use this for initialization
     void Start () {
         paddle = GameObject.FindObjectOfType<Paddle>();
         paddleToBallVector = this.transform.position - paddle.transform.position;
         body = GetComponent<Rigidbody2D>();
     }
     
     // Update is called once per frame
     void Update () {
         if (!hasStarted) { 
             //lock the ball relative to the paddle.
             this.transform.position = paddle.transform.position + paddleToBallVector;
 
             //Wait for a mouse press to launch.
             if (Input.GetMouseButtonDown(0)) {
                 print("mouse Clicked, Launch Ball");
                 hasStarted = true;
                 body.velocity = new Vector2(2f, 10f);
             }
         }
     }
     void OnCollisonEnter2D(Collision2D col) {
         print("!!!!");
         //SimulateWin();
     }
 }
 

Here is my Brick Inspectoralt text

And here is its asociated code

 using UnityEngine;
 using System.Collections;
 
 public class Brick : MonoBehaviour {
 
     public int maxHits;
     private int timesHit;
     private LevelManager levelManager;
 
     // Use this for initialization
     void Start () {
         timesHit = 0;
         levelManager = GameObject.FindObjectOfType<LevelManager>();
     }
     
     // Update is called once per frame
     void Update () {
     }
 
     void OnCollisonEnter2D (Collision2D col) {
         timesHit++;
         print("its a hit");
         SimulateWin();
     }
 
     //TODO Remove this method once we can actually win!
     void SimulateWin() { 
         levelManager.LoadNextLevel();
         }
 }
 

And finally this is my level manager code

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 public class LevelManager : MonoBehaviour {
 
    public void LoadLevel(string name) {
         Debug.Log("level load requested for: " + name);
         SceneManager.LoadScene(name);
     }
     public void QuitRequest() {
         Debug.Log("I want to quit!: ");
         Application.Quit();
     }
     public void LoadNextLevel() {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex +1);
         print("Change Level");
     }
 }
 

ball-inspector.png (55.1 kB)
brick-inspector.png (53.5 kB)
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 saldavonschwartz · Aug 08, 2016 at 09:13 AM 1
Share

The one thing I see is that your ball is non-kinematic but you are still moving it thru it's transform, which you should ideally only do if the rigid body were kinematic.

At least in the 3d physics engine (physX -- the 2d one I believe is Box2d so it might work differently) a non-kinematic body cannot generate collision events. It can receive them though, but for this to happen it has to be woken up by you explicitly moving the object (or by a kinematic body moving it) but only kinematic bodies generate the events.

I do see that you anyway set the bodies to never sleep, so not sure.

I would try applying a force to the ball ins$$anonymous$$d of messing with the transform and body velocity and seeing if when the ball hits the brick either's OnCollisionEnter gets called.

Another two things to check, just in case, are that you did not mess with the physics layers. I see that you have them both in default so assu$$anonymous$$g you did not touch the collision matrix Default collides with Default so it should be fine. And not sure about your game object's hierarchy. But if you have parent-child relationships, those could also modify which object generates (if it generates) events.

avatar image Skeletor101 saldavonschwartz · Aug 21, 2016 at 07:58 PM 0
Share

How would would I put force on the ball, im very new to coding, so i have no idea where to start. Can you show me what the code should look like, and how it would be implemented?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Skeletor101 · Aug 09, 2016 at 04:51 PM

Thanks for responding, Ill look into it and see if this helps.

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.

Follow this Question

Answers Answers and Comments

99 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

Related Questions

Player not dying in apk but does in unity 0 Answers

Adding some offset value for RayCast 2D but what is the distance unit? 0 Answers

Destroying a projectile on collision 1 Answer

unity crashes on start 0 Answers

How to get an access token with the OAuth 2.0 in the Unity Android Application? 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