O'Reilly logo

Unity 3D Game Development by Example by Ryan Henson Creighton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A tiny bit o' math

This code's not going to work. Our paddle moves only to the right, along the positive X-axis, because we're working only with positive numbers. We need some negative numbers in there so that the paddle will move to the left at some point. But, at what point?

Hmm… what if we take half of the screen's width and subtract it from Input.mousePosition.x? What does that do for us?

A quick trip to the Unity Language Reference tells us how to find the width of the screen in pixels. Let's divide that number by two and subtract it from the mouse position.

Change the Debug.Log() function call to look like this:

Debug.Log(Input.mousePosition.x - Screen.width/2);

Save and test. Watch the bottom of the screen for the result.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required