RayCasting Mouse

Hi everyone,

I was just wondering how you would go about RayCasting a mouse in Unity, at the moment I have it attached to the camera and would like to know if it was possible to do this with the mouse. If it isnt could I Raycast via a Object that Follows the mosue.

Code :
ray = new Ray (Camera.main.transform.position, Camera.main.transform.forward);

If you’re working for Editor only then above answer will work . If you’re creating game of iOS/Android then you will need this in the end to cast ray when screen touched .

						#if UNITY_EDITOR
						ray = Camera.main.ScreenPointToRay (Input.mousePosition);
						#elif (UNITY_ANDROID || UNITY_IPHONE)
						 ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);
						#endif
						if (Physics.Raycast (ray, out hit, 100f)) {
							}

Have you tried:

Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		RaycastHit hit;

		if (Physics.Raycast(ray, out hit, 100))
		{
			//Do something here if the mouse is over a object?
		}

Is that what you wanted to do?