- Home /
Dragging an object in iOS (UnityScript)
Does anyone have a solution for dragging colliders in iOS? I tried the following, which I think is not far off, compiles but doesn't work on the device.
var titleCamera : Camera;
var hit : RaycastHit;
var ray : Ray;
function Update () {
if(Input.touchCount == 1) {
ray = titleCamera.ScreenPointToRay(Input.touches[0].position);
var touch : Touch = Input.touches[0];
if (touch.phase == TouchPhase.Moved && Physics.Raycast(ray.origin, ray.direction,hit)) {
var touchPos : Vector2 = Input.GetTouch(0).deltaPosition; // touch position
var newPos : Vector2; //object position
newPos = touchPos;
switch(hit.collider.name){
case "object01":
Debug.Log("object01 drag");
transform.Translate(newPos);
break;
case "object02":
Debug.Log("object01 drag");
transform.Translate(newPos);
break;
}
}
}
}
Any help much appreciated :) It's for detecting a drag on any object using a collider (I've not got around to using tags yet)
Answer by hawken-2 · Jul 13, 2011 at 07:35 AM
trying a few variations on this, but really don't know what to add for newPos. I have a mousedown version of this running just fine, looks like I'm going to have to give up on iPhone touch drag in Javascript for now... here's the code, that functions for mouse, but not for touch;
Any clues as to what to put in the newPos var would be great appreciated!
var titleCamera : Camera;
function OnMouseDown () {
var screenSpace = titleCamera.WorldToScreenPoint(transform.position);
var offset = transform.position - titleCamera.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
while (Input.GetMouseButton(0))
{
var clickPos = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
var newPos = titleCamera.ScreenToWorldPoint(clickPos) + offset;
transform.position = newPos;
yield;
}
}
var hit : RaycastHit;
var ray : Ray;
function Update () {
if(Input.touchCount == 1) {
ray = titleCamera.ScreenPointToRay(Input.touches[0].position);
var touch : Touch = Input.touches[0];
while (touch.phase == TouchPhase.Moved && Physics.Raycast(ray.origin, ray.direction,hit)){
var touchPos : Vector2 = Input.GetTouch(0).deltaPosition;
var newPos = touchPos;
transform.position = newPos;
}
}
}
Answer by efge · Nov 03, 2012 at 12:40 PM
Maybe I am a little bit late ...
That's what the docs say: "OnMouseDown, OnMouseEnter, OnMouseOver, OnMouseExit, OnMouseDown, OnMouseUp, OnMouseDrag events are not supported."
Answer by Anxo · Nov 03, 2012 at 01:09 PM
It might also be worth considering buying something like "Touches" from the asset store. While you CAN write it all yourself, there really is no need unless you need to do something very different. When I got my input touches package it only cost me 10 bucks and saved me a lot of days of trying to work out controls and gave me more time to focus on game design. There are is also the more popular "Finger Gestures" package but it is also more expansive.
I just bought Touches based off of this, but can't get it to do anything that I want! :(
Your answer
Follow this Question
Related Questions
How to push object along Z axis with Ray on touch? 1 Answer
How to hit two object with one raycast? 2 Answers
Help with touch input for iOS please! 1 Answer
Raycast Hit not working 0 Answers
touch game objects on ios 1 Answer