Time for action – declare a variable to store the Screen midpoint
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 we discussed when we looked at naming functions. For example, 1myvar funfun
will not work, because it begins with a number and has a space in the middle. Also, it sounds ridiculous and doesn't make any sense. Try your best to keep your...