[Solved] My paddlePos.x is offset from my mouse cursor

Hi all, at first my cursor was aligned perfectly with my paddle and everything was working fine, up until Section 5 Lesson 92. You can even notice the issue within the first 15 seconds of Lesson 92’s video. When and the paddle is in the center of the screen the mouse will be aligned in the center of the paddle, then when you move the paddle to the left and right you can see the cursor will move further away from the paddle and no longer be aligned with it.

Here is a link to the lecture to see the issue in the first 15 seconds of the video https://www.udemy.com/unitycourse/learn/v4/t/lecture/2073248

I have no idea what could have broken and caused this issue, does anyone know how I can fix it? Here is my code for the paddle.

EDIT:
So I went backtracking in the video Lectures and noticed that the issue does not occur in the beginning of Lecture 87, but it does occur in the beginning of Lecture 88. Perhaps it was introduced in Lecture 87 somehow? I will do some more investigation.

using UnityEngine;
using System.Collections;

public class Paddle : MonoBehaviour {

public bool autoPlay = false;

private Ball ball;

void Start () {
	ball = GameObject.FindObjectOfType<Ball>();
}

// Update is called once per frame
void Update () {
	if (autoPlay == false) {
		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, 0.5f, 15.5f);
	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, 0.5f, 15.5f);
	this.transform.position = paddlePos;
    }
}

Any help would be highly appreciated, thank you! :slight_smile: happy coding.

Well, I’m no coding guru, but, it looks to me that since the variable autoPlay never equal true you can rule out looking the function AutoPlay() for the source of your errors and look solely at MoveWithMouse()

That should narrow down the source of the error.

Now, is that the entirety of the code to do with the Paddle? Your code has deviated a lot from the course material with the addition of the Ball object. And I am not sure what you are trying to achieve with this autoplay, AutoPlay() and MoveWithMouse?

The rest of the code in MoveWithMouse looks similar to mine though. With the removal of the ball object references in your code - as my Ball object has a different name - your code worked fine.

Thank you for your reply Vaughan_MacEgan,

The AutoPlay() method is something that will come up in the further lessons to automate testing.

After reading your reply and seeing how your code was not much different than mine I got the urge to rebuild my game as a Standalone, and guess what, that fixed the issue! This odd issue where the mouse is misaligned with the paddle will only occur if you build the game as Web Player build within Unity 4!

I feel much better now :slight_smile: hehe

I get Unity throwing up so many problems. Sometimes the only answer is to save your work and restart the computer. Don’t you just love Unity :stuck_out_tongue:.

It’s a love hate relationship :wink:

Privacy & Terms