I’m using this code to allow me to drag my objects in my game scene:
// Moves object according to finger movement on the screen
var speed:float = 0.1;
function Update () {
if (iPhoneInput.touchCount > 0 iPhoneInput.GetTouch(0).phase == iPhoneTouchPhase.Moved) {
// Get movement of the finger since last frame
var touchDeltaPosition:Vector2 = iPhoneInput.GetTouch(0).deltaPosition;
// Move object across XY plane
transform.Translate (touchDeltaPosition.x * speed, touchDeltaPosition.y * speed, 0);
}
}
The problem is, that it does not matter where I put my finger on the screen the object is still selected, as you can imagine this will be a problem when the scene has multiple objects.
Is there a simple way to edit the code to make sure the users finger is actually on the object before allowing them to drag it?
I have a vague hunch about leaving out the entire OnMouse… functions, but struggling with understanding the Touch part of UNity.
Any pointers are really welcomed!
Edit: I tried throwing in this code I found in the reference:
// Moves object according to finger movement on the screen
var speed : float = 0.1;
function Update () {
if (Input.touchCount > 0
Input.GetTouch(0).phase == TouchPhase.Moved) {
// Get movement of the finger since last frame
var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
// Move object across XY plane
transform.Translate (-touchDeltaPosition.x * speed,
-touchDeltaPosition.y * speed, 0);
}
}
My objects are touchable, but they just fly like little mentals