- Home /
Out of order Please help
Hello everyone, My problem is i have a ball(sphere). i have added this script to it
function FixedUpdate ()
{
if(Input.GetMouseButton(0))
rigidbody.AddRelativeForce (12, 200, 0);
}
and i have this added to my sphere but no matter where i click it still works. i want it to work only when i click on the sphere. what this script does actually makes the sphere bounce when i click it. Please help!.
Answer by nasa8 · Feb 14, 2014 at 02:15 PM
GetMouseButton triggers whenever you click mouse button doesn't matter where the cursor is not only at the object with attached script. So you should check whether mouse cursor is over your object. You can do that by raycasting. Set Tag of the object to for example "TouchBall" and use:
function FixedUpdate()
{
Ray ray;
RaycastHit hit;
if (Input.GetMouseButton(0))
{
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit))
{
if (hit.collider.tag == "TouchBall")
{
rigidbody.AddRelativeForce (12, 200, 0);
}
}
}
}
Thank u sooooooooooo $$anonymous$$uch. $$anonymous$$an i m new so i don't have so much experience thank you i really appreciate you for giving me time :D