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
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 Inspector
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");
}
}
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.
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?
Answer by Skeletor101 · Aug 09, 2016 at 04:51 PM
Thanks for responding, Ill look into it and see if this helps.
Your answer
Follow this Question
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