[Solved] Block breaker Paddle and Ball

Hi I am working on the block breaker game and each time i click play the paddle and ball jump from the center of the play area to the bottom left when i click the ball shoots but the paddle does not seem to respond


Hi @Paul_Tipper,

Could you select your Background within Playspace in the Hierarchy, so that it’s properties are displayed in the Inspector and pop a screenshot up.

Also, can you post your code up (copy/paste etc) also, or, zip the whole project up - you can upload .zip files to the forum directly.

Hi Here is everything you asked for

Level Manager code

using UnityEngine;
using System.Collections;

public class LevelManager : MonoBehaviour {

	public void LoadLevel(string name){
		Debug.Log ("New Level load: " + name);
		Brick.breakableCount = 0;
		Application.LoadLevel (name);
	}

	public void QuitRequest(){
		Debug.Log ("Quit requested");
		Application.Quit ();
	}
	
	public void LoadNextLevel() {
		Brick.breakableCount = 0;
		Application.LoadLevel(Application.loadedLevel + 1);
	}
	
	public void BrickDestoyed() {
		if (Brick.breakableCount <= 0) {
			LoadNextLevel();
		}
	}
}

Paddle code

using UnityEngine;
using System.Collections;

public class Paddle : MonoBehaviour {

	public bool autoPlay = false;
	public float minX, maxX;

	private Ball Ball;
	
	void Start () {
		Ball = GameObject.FindObjectOfType<Ball>();
	}
		
	// Update is called once per frame
	void Update () {
		if (!autoPlay) {
			MoveWithMouse();
		} else {
			AutoPlay();
		}
	}
	
	void AutoPlay() {
		Vector3 PaddlePos = new Vector3 (0.5f, this.transform.position.y, 0f);
		Vector3 BallPos = Ball.transform.position;
		PaddlePos.x = Mathf.Clamp(BallPos.x, minX, maxX);
		this.transform.position = PaddlePos;
	}
	
	void MoveWithMouse () {
		Vector3 PaddlePos = new Vector3 (0.5f, this.transform.position.y, 0f);
		float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;
		PaddlePos.x = Mathf.Clamp(mousePosInBlocks, minX, maxX);
		this.transform.position = PaddlePos;
	}
}

Ball code

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {
	
	private Paddle paddle;
	private bool hasStarted = false;
	private Vector3 paddleToBallVector;

	// Use this for initialization
	void Start () {
		paddle = GameObject.FindObjectOfType<Paddle>();
		paddleToBallVector = this.transform.position - paddle.transform.position;
	}
	
	// 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;
				this.GetComponent<Rigidbody2D>().velocity = new Vector2 (2f, 10f);
			}
		}
	}
	
	void OnCollisionEnter2D (Collision2D collision) {
		// Ball does not trigger sound when brick is destoyed.
		// Not 100% sure why, possibly because brick isn't there.
		Vector2 tweak = new Vector2 (Random.Range(0f, 0.2f), Random.Range(0f, 0.2f));
		
		if (hasStarted) {	
			GetComponent<AudioSource>().Play();
			GetComponent<Rigidbody2D>().velocity += tweak;
		}
	}
}

and the zip file blockbreaker.zip (7.5 MB)

1 Like

Hi @Paul_Tipper,

Thanks for this… I was going to check the position of the playspace, but decided to look at the project instead, so thanks for attaching the zip file.

This is a nice easy one :slight_smile:

Your Paddle prefab doesn’t have the minX or maxX values set, and they are not set in code either, as such, they are defaulting to zero. The code says “Hey, we have to clamp this paddle between these values” - end result, paddle stuck at zero.

I experimented with Level_01 but as you have the paddle as a prefab you can just apply the changes directly to that and it should resolve it on the other levels also.

minX 0.5
maxX 15.5

Hope this helps :slight_smile:

@Rob well i feel stupid fo missing that thanks again for the help

1 Like

Absolutely no need to feel that way, everyone has been there at some point, and often Paul the longer you look at something the more blind to it you become.

I once spent several hours getting increasingly more frustrated because the player’s score was not being displayed correctly within a scene. I went through my code over and over again, talking to myself, the duck, pretty much anyone who would listen - Gaaah!!!

I took a break, came back to it later and discovered that I hadn’t made the text field in the scene wide enough to display any value above 9 - so my zero place holder was lovely… first score of 100 points never showed… hours I spent on that Paul… hours… lol… funny now… wasn’t at the time :smiley:

1 Like

Privacy & Terms