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 - declare a variable to store the screen midpoint

  1. Modify your Script so that it looks like this (you can get rid of the Debug.Log() function call):
    function Update () {
    var halfW : float = Screen.width/2;
    transform.position.x = (Input.mousePosition.x -halfW)/halfW;
    }
    

That code looks a lot cleaner. Not only is it easier to read, but we've knocked out some of those confusing brackets in the second line.

What just happened we've Gone too Var

We've used the special var keyword to declare our variable (bucket). I chose the name halfW, which is short for "half width" half the width of the screen. You can choose any name you like for a variable as long as it isn't a reserved Unity keyword, and it doesn't break any of the naming rules that ...

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