[SOLVED] Paddle/Player not moving after applying lecture 89 Automated Play Testing changes to the code

Summary:
When starting any of the scenes where the Paddle (I called it Player) is supposed to move along the X axis it does not. Neither using my in-built laptop touch screen nor the USB Mouse actions do not change the position of the Paddle/Player. I am able to launch the ball, however, I am not able to move the Paddle/Player itself.

Additional info:

  • Using Unity 5.4.1.f1 (I have been using it since the very beginning of the course and was able to overcome any minor issues or cross-version differences that occured during the course)
  • Using Visual Studio 2015

Changes introduced to the code:

  • prior to going through lecture #87, #88, #89 - fixed a bug which prevented Players from winning rounds after previously using them (losing did not 0 the breakableCount - fixed it by introducing changes to Lose Collider script:
    FindObjectOfType(); to void start
    Brick.breakableCount = 0; to void OnTriggerEnter2D
    tested the changes, they worked fine. I was quite proud to fix my first bug and doing that totally by myself without access to internet at the time.
  • went through the lectures #87, #88, #89. Introduced the code that initiated Crack and Boing sounds as shown on the video. The problems started on Automated Play Testing when I figured that I can no longer move my Player/Paddle.
  • tried to go back to the state from before fixing (deleted the introduced code - it did not help so I reintroduced it)
  • tried to get rid off the Automated testing script and audio scripts - did not help as well - the Paddle/Player did not move

Repro Steps:

  1. Introduce fix to the issue mentioned above by placing
    FindObjectOfType(); to void start
    Brick.breakableCount = 0; to void OnTriggerEnter2D in Lose Collider script.
  2. Launch Block Breaker state from lecture #89.
  3. Proceed to Scene 01 and try to move the Paddle/Player with your mouse/touch screen.
  4. Observe the issue.

Observed Results:
The Paddle/Player brick does not move along the X axis accordingly to the X axis position of the mouse.

Expected Results:
The Paddle/Player brick should move along the X axis accordingly to the X axis position of the mouse.

My current state of scripts:

Please help :frowning: I would really like to move on with the course as I really like it so far.

Best regards

Is the PlayerController script attached to the Paddle prefab?

Yes it is attached to the prefab. Similarly, Ball script is attached to the Ball prefab, Brick script is attached to Ball prefab, and a Lose Collider script is attached to the Play Space > Lose Collider prefab.

That rules out a nice quick fix then :slight_smile:

I note the script you have is fairly empty, I’m guessing this came from the steps you outlined above? I know the following works, perhaps it might be worth pasting it in and seeing what happens, if it still doesn’t work it will rule out the script.

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

This one obviously includes the code for the autoplay, and exposes some public fields which you will need to populate in the Inspector. I can’t really see anything different from the perspective of just the mouse controlled movement though from your code. I don’t have a touch screen at this end so can’t test that functionality.

1 Like

Thanks a lot!

At first I though that I already implemented the code you provided me with (took it from the projects files provided by Ben) and it did not work.

But since I had nothing to lose I copied your code then filled the minX and maxX in the relevant prefab to 0.5 and 15.5 correspondingly.

It started to work, yet I had NullReference error in the Unity. Then I figured I must have deleted something related to the audio (while I was trying to find the source of the bug previously) and rewritten the Boing part.

Now both the AutoPlay and ordinary mouse play works just fine.

I’m curious what went wrong before, however, I am also happy that I can move forward with my Block Breaker progress.

Once again, thanks!

1 Like

Great news @Damian_Gabrys, I’m glad this has resolved it for you - be sure to share you Block Breaker with us all in the Showcase when you’re ready - I will look forward to playing it :slight_smile:

Privacy & Terms