Paddle does not match mouse position

When I run the Block Breaker game, I the mouse position does not match my paddle (like it does in the video). The paddle starts centered, but after the first frame, it jumps well to the right of my mouse position. It will move left or right as I move the mouse, but the mouse has to move far off the game screen just to center the paddle.

I’ve searched this on the forums and found other people talking about it, but could not fix it.

Tried: Building and running in browser, testing in program.

Version: 5.4.3

Attached is the screenshot of paddle script and object settings.

Thank you!

SOLUTION

I wanted to see what the mouse position number was. So I used this little line of code:
float mouse = (Input.mousePosition.x); print (mouse);

From this, I learned that the mouse position was based on an X Y axis that started its positive numbers at the bottom left of the play screen. So, a centered mouse value was at 400.

I could not find any way to adjust the X,Y axis for Unity, so I just went with it.

If the screen is 16 world units wide, then 8 is the center. Adding - 8 to this line of code, did the trick:

float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16 - 8;

This was also a reason that the paddle would not be constrained to the screed.

If our paddle is one world unit wide, then the max left and right would be -7.5 and 7.5.

I likely screwed up something in the setup process, but I’m glad that I did because it really forced me to dig in and find a fix.

Below is a screen of the code.

11 Likes

Thanks for sharing this

1 Like

I encountered the same problem, thanks so much! :smiley:

3 Likes

I encountered this problem as well today, following along the lesson using the newest version of Unity 5. I messed around with it and ended up coming to this same solution, but came here to see if there was something less messy, cause something seems like it has to be wrong,
The only thing I can think of is several lectures ago I recall Ben changing the anchor point of some stuff to be bottom left instead of center, so when he moves the background image the anchor is at the bottom left. I assume it’s something to do with that, but I couldn’t figure out how to change the anchor point.

If anyone has any further insight on a less messy fix, I’d be interested.

3 Likes

I’ve got the same problem, and I’m pretty sure it needs changing the anchor point of something. I think that the camera and the paddle are in different coordinate systems. The current solution works, but is just a work around. We’re still doing it wrong. Would someone please help?

2 Likes

Had the same problem and guessed it was to do with the anchor point. My background is centered at 0,0 and I think Ben’s is anchored at the lower left hand corner at 0,0. Was trying to readjust my game space to be anchored in the same space as Ben’s but I couldn’t remember how to change the anchor point for objects!

2 Likes

Same Problem Thanks for Help !

1 Like

You bet!

1 Like


maybe this could also help someone to figure out where their mouse position is relative to their paddle.
I changed my subtracted number to better help me get what I needed by creating a variable I could change while running the game. For how I had mine set up, I found subtracting 6 worked for me because I have my game screen set up differently.
: )

1 Like

So I was having the same issue as everyone else here, I’m pretty sure I set everything up correctly but my mouse still wasn’t aligned with the paddle. While printing the X pos I was getting the same 0 , 8 ,16 yet the paddle wasn’t when drag to the same spot my mouse was. So even though when the mouse was there it would show 8 when the paddle would reach the same spot it wouldn’t show up as an 8

After awhile I got tired of trying to figure that out and so I ended up just changing the numbers around, it works for the most part. The mouse won’t stay perfectly aligned in the center of the paddle, it does stay on the paddle though. Before reading this post I tried subtracting 8 but found that the mouse and paddle end up separating after moving them around

Would someone be able to explain why this separation occurs?

1 Like

Very good man, Thanks

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms