- Home /
select and drag an object with touch
Hi, im working on a 2d game.
i've finding that its very complicated to move an object taping it with your finger and then draging it to somewhere else, i looked up on a bunch of sites an a lot of different codes, and i still dont gat that result. I think i'm missing something cause in my opinion that could be one of the main things you want to find when developing a game for a smartphone, i just dont get why its so hard to find how to do that. just recongnize the touch, select an object that its at the spot you've just touched, and then be able to drag it anywhere you want, as the object its copying the touch deltaPosition
this is the last code i've used:
pragma strict
function Update () {
for (var touch : Touch in Input.touches){
var ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).deltaPosition);
var hit : RaycastHit2D;
if ((hit.collider != null)) {
hit.collider.gameObject.transform.position = Touch.deltaPosition;
}
} }
in this case the error is:
NullReferenceException: Object reference not set to an instance of an object
i get this result when i touch the screen.
and i am also getting this:
touch.js(13,52): BCE0022: Cannot convert 'UnityEngine.Ray' to 'UnityEngine.Vector3'.
PLEASE HELP!!!
Answer by supernat · Jun 09, 2014 at 08:18 PM
The easiest approach is to not use raycasts. Attach a collider to the object you want to drag (if not already attached). Add a script to that object with OnMouseDown(), OnMouseUp, and Update() methods. In OnMouseDown, set a flag indicating dragging is active. In the OnMouseUp, reset that flag. Finger touches will be sent through the same Mouse events. Then in Update(), if the drag flag is set, position the game object's transform to the current mouse position (you'll have to figure out your own Z value if it's in 3D).
// Something like this (it's untested, there may be some extra work involved, but the idea is generally the following and
// it should work for an orthographic 2D camera if Z is your fwd/back axis, 3D gets more complicated,
// because the Z value is not necessarily into/out of screen
void Update() {
if (dragging) {
Vector3 posn = Camera.main.ScreenToWorldPoint(Input.mousePosition);
posn.z = 0; // or whatever depth you want the game object to remain at, you could save it off ahead of time and reset it here
transform.position = posn;
}
}
Obviously, if you're doing this in 3D, it's much more complicated, because you have to define a depth to place the object at. If doing that, the way I would approach it is to calculate the distance to the object in the OnMouseDown event, then when you get a returned position from ScreenToWorldPoint, you normalize that value and extend it by the distance you saved off, so the object will remain at the same distance from the player no matter where they drag it.
sorry, when you say a "flag", do you mean a boolean variable?
Your answer
Follow this Question
Related Questions
How to hit two object with one raycast? 2 Answers
Multiple hit detection with Raycasting? 2 Answers
Touch script using raycast on a collider.. :( 0 Answers
Dragging an object in iOS (UnityScript) 3 Answers