Search Unity

How to drag an object

Discussion in 'iOS and tvOS' started by numeric, Jun 22, 2010.

  1. numeric

    numeric

    Joined:
    Jan 31, 2010
    Posts:
    170
    Hey guys,

    I'm using this code to allow me to drag my objects in my game scene:

    Code (csharp):
    1.  
    2. // Moves object according to finger movement on the screen
    3. var speed:float = 0.1;
    4. function Update () {
    5.     if (iPhoneInput.touchCount > 0  iPhoneInput.GetTouch(0).phase == iPhoneTouchPhase.Moved) {
    6.    
    7.         // Get movement of the finger since last frame
    8.         var touchDeltaPosition:Vector2 = iPhoneInput.GetTouch(0).deltaPosition;
    9.        
    10.         // Move object across XY plane
    11.         transform.Translate (touchDeltaPosition.x * speed, touchDeltaPosition.y * speed, 0);
    12.     }
    13. }
    14.  
    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?

    Thanks
     
  2. numeric

    numeric

    Joined:
    Jan 31, 2010
    Posts:
    170
    I'm stuggling to get past this, people have mentioned raycasting before on the forums, could this be appropriate?
     
  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    There is a simple raycast example in this thread. Also, you might want to look at adding an OnMouseDown function to the draggable object's script.
     
  4. numeric

    numeric

    Joined:
    Jan 31, 2010
    Posts:
    170
    Thanks andeeee that worked nicely.

    I'm having a problem implementing the draging part of the script, heres what I mangaged to do:

    http://pastie.org/1014997

    Thanks
     
  5. numeric

    numeric

    Joined:
    Jan 31, 2010
    Posts:
    170
    I still cant seem to implement the dragging action, any thoughts on how to tackle this?

    Thanks
     
  6. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I've attached a script that shows a way to drag an object in the plane of the camera. You can add it to any convenient object in the scene.
     

    Attached Files:

  7. numeric

    numeric

    Joined:
    Jan 31, 2010
    Posts:
    170
    Thanks that works great :)

    The only problem is that all objects become draggable, not just the one it is attached to?
     
  8. numeric

    numeric

    Joined:
    Jan 31, 2010
    Posts:
    170
    Any ideas on this ?

    Thanks :)
     
  9. numeric

    numeric

    Joined:
    Jan 31, 2010
    Posts:
    170
    Heres a video of the problem : http://tiny.cc/2v6ux

    As you can see , as I move my mouse across the wall I seem to somehow allow cubes to pass through them -as if I'm moving the collider?

    Although I can also move cubes ( which is the part I want)

    I really dont know whats wrong with the code. I have added in 2 extra checks and that did not make a difference.

    Code (csharp):
    1.  
    2. private var currTarget: Transform;
    3. private var dragging: boolean;
    4. private var clickOffset: Vector3;
    5.  
    6.  
    7. function Update () {
    8.     var objPlane: Plane;
    9.     var hitDist: float;
    10.     var hit: RaycastHit;
    11.  
    12.  
    13.             if (Input.GetMouseButton(0)  gameObject.name =="enemy")  {
    14.         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    15.  
    16.         if (dragging) {
    17.             objPlane = new Plane(Camera.main.transform.forward, currTarget.position);
    18.             objPlane.Raycast(ray, hitDist);
    19.             currTarget.position = ray.GetPoint(hitDist) + clickOffset;
    20.         } else if (Physics.Raycast(ray, hit)  Input.GetMouseButtonDown(0) gameObject.name =="enemy") {
    21.             dragging = true;
    22.             currTarget = hit.transform;
    23.            
    24.             objPlane = new Plane(Camera.main.transform.forward, currTarget.position);
    25.             objPlane.Raycast(ray, hitDist);
    26.             clickOffset = currTarget.position - ray.GetPoint(hitDist);
    27.         }
    28.     } else {
    29.         dragging = false;
    30.     }
    31. }
    32.  
     
  10. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Sorry, I should have thought of that...

    Anyway, the best place to check is on the "else if" line:-
    Code (csharp):
    1. else if (Physics.Raycast(ray, hit)  Input.GetMouseButtonDown(0)  hit.gameObject.name == "enemy") { ...
    This is how you would identify the target by name, but you could just as easily do it by the tag, etc.
     
  11. numeric

    numeric

    Joined:
    Jan 31, 2010
    Posts:
    170
    Thanks andeee, I seem to be getting this error when I make the change:

    'gameObject' is not a member of 'UnityEngine.RaycastHit'
     
  12. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    D'Oh! Sorry, it should be "hit.transform.name", not "hit.gameObject.name".
     
  13. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    Andeeee: Im struggling a bit to convert your nice dragObject script into something I can use for my Xoom.

    I changed something in your script here:
    Code (csharp):
    1. function OnMouseDown ()
    2. {
    3.     [B]if(Input.touchCount > 0)[/B]
    4.     {
    5.     canMove = true;
    6.     myTransform.Translate(Vector3.up*addHeightWhenClicked);
    7.     gravitySetting = myRigidbody.useGravity;
    8.     freezeRotationSetting = myRigidbody.freezeRotation;
    9.     myRigidbody.useGravity = false;
    10.     myRigidbody.freezeRotation = freezeRotationOnDrag;
    11.     yPos = myTransform.position.y;
    12.     }
    13. }
    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:
    Code (csharp):
    1. // Moves object according to finger movement on the screen
    2.  
    3. var speed : float = 0.1;
    4. function Update () {
    5. if (Input.touchCount > 0
    6. Input.GetTouch(0).phase == TouchPhase.Moved) {
    7.  
    8. // Get movement of the finger since last frame
    9. var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
    10.  
    11. // Move object across XY plane
    12. transform.Translate (-touchDeltaPosition.x * speed,
    13. -touchDeltaPosition.y * speed, 0);
    14. }
    15. }
    My objects are touchable, but they just fly like little mentals :)
     
    Last edited: Aug 8, 2011