x


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!

more ▼

asked Apr 17, 2011 at 05:47 AM

Earth-O-Matic gravatar image

Earth-O-Matic
70 27 18 27

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Apr 17, 2011 at 08:09 AM

Mortennobel gravatar image

Mortennobel
1.9k 24 25 50

I redid the code and now I can click and drag.

Apr 18, 2011 at 02:39 AM Earth-O-Matic
(comments are locked)
10|3000 characters needed characters left

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));
        }
    }
}
more ▼

answered Apr 18, 2011 at 02:41 AM

Earth-O-Matic gravatar image

Earth-O-Matic
70 27 18 27

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2187
x1094
x449
x332

asked: Apr 17, 2011 at 05:47 AM

Seen: 2355 times

Last Updated: Apr 17, 2011 at 05:47 AM