Search Unity

How to Drag Drop Object Whose Movement is Restricted

Discussion in 'Scripting' started by giza, May 19, 2009.

  1. giza

    giza

    Joined:
    May 4, 2008
    Posts:
    69
    I'm trying to put together an abacus type of game. The user moves spheres along a rod from right to left and vice versa. My problem is how to restrict movement to the x-axis but still be able to drag the spheres along the rod.

    I have the following drag script on each sphere:
    Code (csharp):
    1. var curPos =0;
    2. var curScreenPos : Vector3;
    3. var trigger = false;
    4. var held_obj = false;
    5.  
    6. function OnMouseDown () {
    7.    var screenPos = Camera.main.WorldToScreenPoint(transform.position);
    8.    var offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPos.z));
    9.    
    10.    //While Loop when the mouse is held down
    11.    while (Input.GetMouseButton(0))
    12.    {
    13.       var curScreenPos = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPos.z);
    14.       var curPos = Camera.main.ScreenToWorldPoint(curScreenPos) + offset;
    15.       transform.position = curPos;
    16.       yield;
    17.      
    18.       held_obj = true;
    19.        
    20.    }
    21. }
    I have added configurable joints to each sphere and locked the y and z motion features but the speheres tend to bunch up on top of each other.

    I have also attached the following script to the spheres but this doesn't seem to have any effect:


    Code (csharp):
    1. var rigidMotion : Rigidbody;
    2.  
    3. function Start()
    4. {
    5.    rigidMotion = rigidbody;
    6. }
    7.  
    8. function Update()
    9. {
    10.    rigidMotion.position.y = 0;
    11.    rigidMotion.position.z = 0;
    12.    rigidMotion.position.x = 0;
    13.    rigidMotion.position = Vector3.zero;
    14. }
    I have also tried "boxing" the spheres in using big box colliders to try to keep them in place but this isn't ideal either.

    Any help in sorting out this problem would be highly appreciated!
     
  2. giza

    giza

    Joined:
    May 4, 2008
    Posts:
    69
    Nobody have any ideas at all? That's weird!
     
  3. giza

    giza

    Joined:
    May 4, 2008
    Posts:
    69
    I got it. I had too many colliders in the scene and the code was obviously wrong. Fixed it with the following on each sphere and the higherLimit and lowerLimit vales based on the length of the rod:

    Code (csharp):
    1. var rigidMotion : Rigidbody;
    2. var higherLimit = -6.628249;
    3. var lowerLimit = -1.521839;
    4.  
    5. function Start()
    6. {
    7.   rigidMotion = rigidbody;
    8. }
    9.  
    10. function Update()
    11. {
    12.   rigidMotion.transform.position.x = -1.826413;
    13.    rigidMotion.transform.position.y = 10;
    14.    
    15.      if (transform.position.z < higherLimit)
    16.      {
    17.       transform.position.z = higherLimit;
    18.      }
    19.         else if (transform.position.z > lowerLimit)
    20.        {
    21.        transform.position.z = lowerLimit;
    22.        }    
    23.    
    24. }