Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

Click n drag iphone?

Discussion in 'iOS and tvOS' started by mookler, Jan 31, 2009.

  1. mookler

    mookler

    Joined:
    Jan 26, 2009
    Posts:
    34
    Hi,

    Im a newbie, can someone point me in the right direction for:

    1. Click and drag an object when finger is pressed on it.

    2. Some iPhone scripting resources on the web.

    Thanks!
     
  2. mehware

    mehware

    Joined:
    Nov 19, 2007
    Posts:
    739
    If you make your object a rigidbody and apply the DragRigidbody script to the main camera it will do what you want.
     
  3. mookler

    mookler

    Joined:
    Jan 26, 2009
    Posts:
    34
    ok, but I want it do only be dragable along the xaxis, and that script is large and hard for me to modify..
     
  4. ivanmoen

    ivanmoen

    Joined:
    Dec 26, 2008
    Posts:
    102
    You say apply the DragRigidbody script… Where do I find this? When I create a new project in Unity iPhone the Project view is empty. Is it supposed to be like this? If I do the same in ‘regular’ Unity, I get two folders ‘Pro Standard Assets’ and Standars Assets’ which is full of stuff.

    I hope this isn’t a stupid question… I’m completely new to Unity :roll:
     
  5. mehware

    mehware

    Joined:
    Nov 19, 2007
    Posts:
    739
    ok do this,

    add the DragRigidbody.js to the maincamera.

    Your rigidbody that you want to drag make a script (JS) and in Update() add,

    Code (csharp):
    1. transform.position.y = 0;
    2. transform.position.z = 0;
    Attach this script whatever you call it to the object (your rigidbody) you want to move just along the x-axis.
     
  6. mookler

    mookler

    Joined:
    Jan 26, 2009
    Posts:
    34
    This does not work, anyone else have any ideas?
     
  7. robbmcmahan

    robbmcmahan

    Joined:
    Feb 25, 2009
    Posts:
    99
    I'm trying to use this script but it's acting "wonky" and i'm not sure it's what I need. First of all, I have multiple rigid bodies in the scene but I only want to drag one of them. How can I do that with this script? Also, it seems like it lags a lot. Instead of dragging, I just really need it to move with the finger on the screen.

    Any help would be greatly appreciated.

    Thanks!
     
  8. codinghero

    codinghero

    Joined:
    Mar 21, 2009
    Posts:
    450
    There doesn't seem to be much more going on in this subject so I figured I'd post my $0.02. The script below is derived from previous examples, however this will make sure you only drag the object you selected. Also, the object you select will be dragged until yo lift your finger off the screen.

    If you only want to be able to drag one object, you can put this on an empty game object, camera, etc. and drop the desired object into the "object" slot. For multiple dragable objects, just drop it onto each one (and still drop that object onto the "object" slot).

    I'm only using single plane dragging (x,y is in the code, just commented out), so I'm sure you will need to tweak the values a bit if you want to enable x,y dragging.

    Code (csharp):
    1. #pragma strict
    2.  
    3. var object : GameObject = null;
    4.  
    5. private var oT : Transform = null;
    6. private var selected : boolean = false;
    7.  
    8.  
    9. function Awake() {
    10.     if (object) {
    11.         oT = object.transform;
    12.     } else {
    13.         var script : TouchDrag = GetComponent(TouchDrag);
    14.         script.enabled = false;
    15.     }
    16. }
    17.  
    18. function FixedUpdate () {
    19.     if (object) {
    20.         var cam : Camera = Camera.main;
    21.         for (var touch : iPhoneTouch in iPhoneInput.touches) {
    22.             var ray = Camera.main.ScreenPointToRay(touch.position);
    23.             var hit : RaycastHit;
    24.  
    25.             if (touch.phase == iPhoneTouchPhase.Began) {
    26.                 if (Physics.Raycast(ray, hit, 100)) {
    27.                     if (hit.collider.gameObject == object) {
    28.                         selected = true;
    29.                     }
    30.                 }
    31.             } else if (touch.phase == iPhoneTouchPhase.Moved) {
    32.                 if (selected == true) {
    33.                     var cameraTransform = cam.transform.InverseTransformPoint(0, 0, 0);
    34.                     var touchPosition = cam.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, cameraTransform.z));
    35.  
    36.                     oT.position.x = touchPosition.x;
    37.                     /**
    38.                      * replace the line above with this one to enable full x,y positioning
    39.                      * oT.position = touchPosition;
    40.                      */
    41.                 }
    42.             } else if (touch.phase == iPhoneTouchPhase.Ended) {
    43.                 selected = false;
    44.             }
    45.         }
    46.     }
    47. }
    ____________________
    -james
     
  9. rocket5tim

    rocket5tim

    Joined:
    May 19, 2009
    Posts:
    237
    motojt, I'm getting the following error on that script in Unity iPhone:

    Assets/NewBehaviourScript 1.js(13,20): BCE0018: The name 'TouchDrag' does not denote a valid type.
     
  10. CoCoNutti

    CoCoNutti

    Joined:
    Nov 30, 2009
    Posts:
    513
    <<Bumping>>

    Anyone have any updates to this error or suggestions? Would like to see this script in action.

    I'm not much of a coder, but does the TouchDrag problem a reference to a script or component?
     
  11. vogles

    vogles

    Joined:
    Sep 28, 2009
    Posts:
    131
    i believe the script itself is named "TouchDrag.js". I think what Awake() is trying to do is find the transform for the object passed in. if it doesn't find it, then disable the script so you don't get any errors or warnings.
     
  12. codinghero

    codinghero

    Joined:
    Mar 21, 2009
    Posts:
    450
    Yeah... I've been away for a bit. :p

    Vogles is correct in that the name of the script is TouchDrag.js. I actually use this exact code in my game Pinbowl, with exception of one line that is of no consequence.

    As stated before I just created an empty object and dragged the script onto it. I then dragged the ball (in Pinbowl) into the "object" variable in that empty game object. That object is also home to my static game controller, which is why I decided to do it that way. Also, remember that I am only dragging on the x axis for my game, so to allow x,y you must replace the line as stated in my previous post.

    Here is the final version as exists in my game:

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var object : GameObject = null;
    5.  
    6. private var oT : Transform = null;
    7. private var selected : boolean = false;
    8.  
    9.  
    10. function Awake() {
    11.     if (object) {
    12.         oT = object.transform;
    13.     } else {
    14.         var script : DragObject = GetComponent(DragObject);
    15.         script.enabled = false;
    16.     }
    17. }
    18.  
    19. function FixedUpdate () {
    20.     if (object) {
    21.         var cam : Camera = Camera.main;
    22.         for (var touch : iPhoneTouch in iPhoneInput.touches) {
    23.             var ray = cam.ScreenPointToRay(touch.position);
    24.             var hit : RaycastHit;
    25.  
    26.             if (touch.phase == iPhoneTouchPhase.Began) {
    27.                 if (Physics.Raycast(ray, hit, 100)) {
    28.                     if (hit.collider.gameObject == object) {
    29.                         selected = true;
    30.                     }
    31.                 }
    32.             } else if (touch.phase == iPhoneTouchPhase.Moved) {
    33.                 if (selected == true) {
    34.                     var cameraTransform = cam.transform.InverseTransformPoint(0, 0, 0);
    35.                     var touchPosition = cam.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, cameraTransform.z));
    36.  
    37.                     oT.position.x = touchPosition.x;
    38.                 }
    39.             } else if (touch.phase == iPhoneTouchPhase.Ended) {
    40.                 selected = false;
    41.             }
    42.         }
    43.     }
    44. }
    45.  
     
  13. mediashock

    mediashock

    Joined:
    Apr 15, 2009
    Posts:
    65
    I;ve been trying to figure this out for few days and cant get it to work.

    I have no errors at all.. if I do a trace call inside the fixedupdate() call it keeps calling but not inside

    if (touch.phase == iPhoneTouchPhase.Began) {
    or anywhere else.


    any ideas??
    I have a emtpy gameobject with this script on it and dragged in a box with rigidbody physics attached to it
     
  14. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Can you post the script you've got currently?
     
  15. groovfruit

    groovfruit

    Joined:
    Apr 26, 2010
    Posts:
    257
    Hi

    I attached the above script to an empty game object then assigned the main camera to this script. It didn't work as i had hoped.....

    Any tips? I just want to move the camera along X with finger drag.

    Cheers
     
  16. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    There's an example in this thread showing how to use raycasting to get the world space position of an object under the mouse/finger position onscreen. Having got the world space position, just get its X coordinate and set the draggable object's X coordinate to the same value.
     
  17. jblazer

    jblazer

    Joined:
    Apr 17, 2009
    Posts:
    31
    Hi - I too am trying to get a good touch and drag going for a game, but need help with one thing. I've been successful in getting this script to work and added the last 4 lines of code (and two more variables declared at the top):

    1. I've found increasing the size of the collider greatly helps in being able to grab an object. In my case, increasing the default size to 1.5 units did the trick. After the touch has released though, and I want to reactivate physics, I reduce the radius back to .5

    2. Also, to get accurate touch and drag working your fixed timestep (Edit->Project Settings->Time) needs to be lowered to .01

    3. Working on the isKinematic flag right now... I'd like to have isKinematic = true at start so that physics are not controlling the object, and then when the user drops the object for physics to take over... Here's where I'm stuck. It works for one object, but if I have multiple objects on screen that have this script attached (either directly or to an empty GameObject), when I drag and drop one object it turns all objects isKinematic flags to false at once, and not just the GameObject specified in the obj variable?!? Any ideas? Maybe I'm missing something simple? Attached is an example package with touch and drag implemented...


    [edit] see code below for fix...
     

    Attached Files:

  18. jblazer

    jblazer

    Joined:
    Apr 17, 2009
    Posts:
    31
    I fixed it... Had to do with keeping the object selected...

    Here's the working script:


    Code (csharp):
    1. #pragma strict
    2.  
    3. var obj : GameObject;
    4. private var radius : float;
    5. private var oT : Transform = null;
    6. private var selected : boolean = false;
    7. private var controller : SphereCollider;
    8. private var isKinematic : boolean;
    9. function Awake() {
    10.    if (obj) {
    11.       oT = obj.transform;
    12.      
    13.    } else {
    14.       var script : TouchDrag = GetComponent(TouchDrag);
    15.       script.enabled = false;
    16.    }
    17. }
    18.  
    19. function FixedUpdate () {
    20.    if (obj) {
    21.       var cam : Camera = Camera.main;
    22.       for (var touch : iPhoneTouch in iPhoneInput.touches) {
    23.          var ray = Camera.main.ScreenPointToRay(touch.position);
    24.          var hit : RaycastHit;
    25.  
    26.          if (touch.phase == iPhoneTouchPhase.Began) {
    27.             if (Physics.Raycast(ray, hit, 100)) {
    28.                if (hit.collider.gameObject == obj) {
    29.                   selected = true;
    30.                }
    31.             }
    32.          } else if (touch.phase == iPhoneTouchPhase.Moved) {
    33.             if (selected == true) {
    34.                var cameraTransform = cam.transform.InverseTransformPoint(0, 0, 0);
    35.                var touchPosition = cam.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, cameraTransform.z));
    36.  
    37.                oT.position.x = touchPosition.x;
    38.                        
    39.                          }
    40.          } else if (touch.phase == iPhoneTouchPhase.Ended) {
    41.             if (selected == true) {
    42.                 controller = GetComponent(SphereCollider);
    43.           controller.radius = 0.5;
    44.             obj.rigidbody.isKinematic = false;
    45.            obj.rigidbody.WakeUp();
    46.             selected = false;
    47.            
    48.                      }
    49.       }
    50.    }
    51. }
    52. }
     
  19. Zoneman

    Zoneman

    Joined:
    Feb 6, 2010
    Posts:
    42
    This happened to me too.
    When I named the script file "TouchDrag.js", it worked for me.


     
  20. crevelop

    crevelop

    Joined:
    Nov 9, 2010
    Posts:
    13
    Last edited: Jan 20, 2013
  21. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    Thnx Crevelo :D
     
unityunity