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

Time for action - follow the Y position of the mouse

  1. Modify your code to add a few familiar-looking lines:
    function Update ()
    {
    var halfW:float = Screen.width/2;
    transform.position.x = (Input.mousePosition.x - halfW)/halfW;
    var halfH:float = Screen.height/2;
    transform.position.z = (Input.mousePosition.y - halfH)/halfH;
    }
    

The two new lines of code are almost identical to the first two lines. We've created a new variable and called it halfH (half height) instead of halfW. We're changing the z property of Input.mousePosition instead of x. When you save the Script and test your game, you'll have a fully movable paddle to bounce your ball on.

Tip

Math effect

I actually put a little cheat into my code. I wanted the paddle to travel deeper and farther ...

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