- Home /
Click Drag not working on iphone
Hey again all. I have a click drag script here that runs fine otherwise and doesn't produce errors but once I get the game on my iphone it just doesn't work. Any one have an idea why? Here's the code.
C#....
using UnityEngine; using System.Collections;
[RequireComponent(typeof(MeshCollider))]
public class clickDragMesh : MonoBehaviour {
private Vector3 screenPoint; private Vector3 offset;
void OnMouseDown() { screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag() { Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset; transform.position = curPosition;
}
}
As always, thank you for the help!
Answer by Mortennobel · Apr 17, 2011 at 08:09 AM
You cannot use mouse controls on your iPhone, but has to use touch events instead. The touch events can be read from the Input class.
http://unity3d.com/support/documentation/ScriptReference/Input-touches.html
This means that you have to code the "mouse drag" functionality yourself.
Answer by Earth-O-Matic · Apr 18, 2011 at 02:41 AM
Here is the code if anyone is interested. I'm still having trouble with stopping the drag and doping the object when I lift my finger. This is just the drag code I used. I'm using a modified version of this code currently to touch and drop - no drag. Would like drag though if I can figure it out or someone else can chime in.
#pragma strict
var cam : Camera; var hit : RaycastHit;
function Start() { cam = Camera.main; }
function Update () { if(iPhoneInput.touchCount == 1) { var touchPos : Vector2 = iPhoneInput.touches[0].position; var ray : Ray = cam.ScreenPointToRay(touchPos);
if(Physics.Raycast(ray,hit,100))
{
transform.position = cam.ScreenToWorldPoint(Vector3(touchPos.x,touchPos.y,10));
}
}
}