Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problems with drag and drop using raycast

Discussion in 'Scripting' started by Boog1eMan, Apr 17, 2014.

  1. Boog1eMan

    Boog1eMan

    Joined:
    Oct 6, 2012
    Posts:
    1
    Hey guys. I'm making a game in which the main character has a rope-like arm (using character joints), which the player can drag around the screen (hit enemies).
    The problem is that if the player drags the arm too far away the arms start to jiggle uncontrollably (they return to their original position afterwards). I've tried to fix it in 2 different ways.

    1. Calculate the range between the fist and the shoulder joint using pythagoras (getLength()-function in the code) and comparing it to various values (~4.1 which is a bit longer than the original arm length). Idea was if it was dragged too far away it would release the arm.
    2. Set a timer on how long the player can drag the fist. The idea in this was if player can only drag it for few milliseconds, it wont have time to start jiggling.

    Code (csharp):
    1. #pragma strict
    2.  
    3. private var object : Transform;
    4. private var offSet : Vector3;
    5. private var dist : float;
    6. var root : Transform;
    7. var test : float;
    8.  
    9. function Update () {
    10.    
    11.     var ray = camera.ScreenPointToRay(Input.mousePosition);
    12.    
    13.     if (Input.GetButtonDown("Fire1")) {
    14.         if (!object) {
    15.             var hit : RaycastHit;
    16.            
    17.             if (Physics.Raycast(ray, hit, Mathf.Infinity)  (hit.collider.tag == "Draggable"))
    18.             {
    19.                 object = hit.transform; // i think this is the problem
    20.                 offSet = object.position-hit.point;
    21.                 dist = (ray.origin - hit.point).magnitude;
    22.             }
    23.         }
    24.     }
    25.    
    26.     else if (Input.GetButtonUp("Fire1"))
    27.     {
    28.         object = null;
    29.     }
    30.     if (object)
    31.     {
    32.         object.position = ray.GetPoint(dist) + offSet;
    33.     }
    34. }
    35.  
    36. function getLength(object : Transform) : float
    37. {
    38.     var length : float;
    39.     var rootx : float = root.transform.position.x - object.transform.position.x;
    40.     var rooty : float = root.transform.position.y - object.transform.position.y;
    41.     var rootz : float = root.transform.position.z - object.transform.position.z;
    42.    
    43.     length = Mathf.Sqrt((rootx*rootx)+(rooty*rooty)+(rootz*rootz));
    44.     return length;
    45. }
    I suspect the problem is in object = hit.transform on line 19. It seems like while the player is dragging the fist it's continually on that line, so using while loop or if, doesn't work (At least i couldn't make it work, unity froze. Probably eternal loop or something). Same with the timer. Any ideas how to make either of those solutions work or perhaps any other ones?

    [FIXED] Misunderstood how this script works, fixed by adding timer inside if (object) statement and if the timer ran out then it would set object back to null aka release the fist. Originally was planning on simulating a mouse button release via code but didn't find an easy way to do that.
     
    Last edited: Apr 18, 2014