Graphics Reference
In-Depth Information
// have to access them every time we need to relay input
// data out
// calculate a percentage amount to use per pixel
float scalerX = 100f / Screen.width;
float scalerY = 100f / Screen.height;
// calculate and use deltas
float mouseDeltaY = Input.mousePosition.y - prevMousePos.y;
float mouseDeltaX = Input.mousePosition.x - prevMousePos.x;
// scale based on screen size
vert += ( mouseDeltaY * speedY ) * scalerY;
horz += ( mouseDeltaX * speedX ) * scalerX;
// store this mouse position for the next time we're here
prevMousePos= Input.mousePosition;
// set up some Boolean values for up, down, left and right
Up = ( vert>0 );
Down = ( vert<0 );
Left = ( horz<0 );
Right = ( horz>0 );
// get fire / action buttons
Fire1= Input.GetButton( "Fire1" );
}
public void LateUpdate()
{
// check inputs each LateUpdate() ready for the next tick
CheckInput();
}
}
4.7.1.1 Script Breakdown
The Start() function adds a default value to prevMousePos, which will be used to calcu-
late the movement between each update. If prevMousePos was left at zero (its default), then
the first time CheckInput() happens, the script will incorrectly assume that the mouse has
moved from 0,0 to the current mouse position.
Input provides an interface to Unity's input systems. It is used for everything from
gyrometer input on mobile devices to keyboard presses. Input.mousePosition delivers the
current position of the mouse in pixel coordinates as a 2D vector, with the bottom left of
the screen at 0,0 and the top right at Screen.width, Screen.height.
public void Start ()
{
Search WWH ::




Custom Search