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");
}
}