OnMouseDown and OnMouseDrag with RayCast

I’ve been working on a clone similar to fruit slice and I’ve been having a problem with onMouseDown and onMouseDrag working 100% correctly. Right now if I click and drag on the screen before it touches an object (e.g. the fruit) it will correctly slice as many objects as I want. It’s using a RayCast to figure out whether it has touched the object and then it will change the graphic on the object after it has touched. The problem comes in when I begin my slice on the actual object. It’ll change the animation of the object that I initallity touched; however, it won’t let me continue to ‘slice’ any of the other objects. The RayCast seems to stop working after the first object is touched directly until I release the mouse and then re-click another object. Then it goes back into the same pattern, if I don’t click the object directly first it’ll continue to slice, or if I do touch the object directly it’ll only allow the first object to be detected.

Here is the onMouseDrag code that I’ve been using:

   RaycastHit hit;
   if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
   {
       Transform objecthit = hit.transform;
       if (hit.transform.gameObject.tag == "testObject")
       {
           //Change material
       }
   }

I’ve also included a getMouseButtonDown statement in my Update(), since that was the only way I could get it to at least change the material on the initial click of the object. If I didn’t add this in nothing would happen if I clicked the object directly:

 void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
            {
                Transform objecthit = hit.transform;
                if (hit.transform.gameObject.tag == "testObject")
		        {
		        //Change Material
		        }
	        }
        }
    }

Is there a way to get the RayCast to continue to detect even if the object is touched directly?

Try replacing Input.GetMouseButtonDown(0) with Input.GetMouseButton(0).

Input.GetMouseButtonDown(0) returns true only during the first frame when the user presses the left mouse button. However, Input.GetMouseButton(0) returns true while the left mouse button is being held down. I hope that makes sense.
Good luck with your game! I love Fruit Slice/Fruit Ninja (iOS) too. :slight_smile: