OnMouseDown()

I would like to replace the OnMouseDown() function to use it on iPhone. How do i rescript it. Please suggest.

function OnMouseDown() as per the document This function has no effect on iPhone.

Thank you.

It uses a raycast so you would have to do that manually. Here place this script onto an empty game object. It will enable OnMouseDown events to occur on the iPhone. Note that this will cause OnMouseDown() to fire twice when not on the iPhone though.

// OnMouseDownFix.js
// Place this on an empty game object.
// It will allow OnMouseDown() function to be received by all game objects.

function Update () {
	if (Input.GetMouseButtonDown (0)) {
		var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		var hit : RaycastHit;
		if (Physics.Raycast (ray, hit))
			hit.collider.SendMessage ("OnMouseDown");
	}
}

What about Android? Thanks :slight_smile:

yes, it SHOULD work on the android as well :slight_smile:

Thanks!

Can you help me with this script? I want to use this in my game (for android) and i totally have no idea how to rescript it. I’m new to this ^^

// Attach this script to an orthographic camera.
private var object : Transform;		// The object we will move.
private var offSet : Vector3;		// The object's position relative to the mouse position.

function Update () {
	var ray = camera.ScreenPointToRay(Input.mousePosition);		// Gets the mouse position in the form of a ray.
	if (Input.GetButtonDown("Fire1")) {		// If we click the mouse...
		if (!object) {		// And we are not currently moving an object...
			var hit : RaycastHit;
			if (Physics.Raycast(ray, hit, Mathf.Infinity)) {		// Then see if an object is beneath us using raycasting.
				object = hit.transform;		// If we hit an object then hold on to the object.
				offSet = object.position-ray.origin;		// This is so when you click on an object its center does not align with mouse position.
			}
		}
	}
	else if (Input.GetButtonUp("Fire1")) {
		object = null;		// Let go of the object.
	}
	if (object) {
		object.position = Vector3(ray.origin.x+offSet.x, object.position.y, ray.origin.z+offSet.z);		// Only move the object on a 2D plane.
	}
}

Will this work on GUI as well?