Game Development Reference
In-Depth Information
//if the ray hits an object and that
//object is a ball
if(Physics.Raycast (ray, hit, 100) &&
hit.transform.gameObject.tag == "ball")
{
//take hold of it with the focusObj variable
focusObj = hit.transform.gameObject;
//remember the location of the mouse
lastMousePosition = Input.mousePosition;
}
}
//if the fire button is down
if(focusObj && Input.GetButton("Fire1"))
{
//calculate how far the the mouse has moved across
//the screen
deltaMousePosition = Input.mousePosition -
lastMousePosition;
//use this value to set the position of the object
focusObj.transform.position.x + =
deltaMousePosition.x/mouseSensitivity;
//remember the location of the mouse
lastMousePosition = Input.mousePosition;
}
//if the fire button is released
if(focusObj && Input.GetButtonUp("Fire1"))
{
//forget about the object that was clicked on
focusObj = null;
}
}
Step 2. Play. You will now be able to move the balls horizontally to
line them up with the same colored beakers by clicking on them and
dragging the mouse. To change the mouse sensitivity, modify the
related variable at the top of the script. Note that it is exposed
so that you can change it easily in the Inspector.
Step 3. The ability to move the ball once it has gone below the white
line (fashioned from the cube added at the beginning) needs to be
added next. The easiest way to do this is to change the ball's tag as it
passes through the trigger collider of the cube. To achieve this, create
a new JavaScript file called stopBalls and add the code in Listing 4.7 .
Attach this script to the cube.
Search Nedrilad ::




Custom Search