Search Unity

How to detach a rigidbody on particular touch

Discussion in 'Scripting' started by gokulreddy, May 19, 2015.

  1. gokulreddy

    gokulreddy

    Joined:
    Apr 2, 2014
    Posts:
    11
    I have an issue in detaching single rigidbody(small ball),when i touch on one blue sprite,ball attached to the sprite should be detached but when i hit on any one sprite both the balls are detaching.please help me out.Thanks in advance
    Code (JavaScript):
    1. var ball : Rigidbody;
    2. var Position:Transform;
    3.  
    4. function Update () {
    5.         for (var i = 0; i < Input.touchCount; i++)
    6.         {
    7.            if (Input.GetTouch(i).phase == TouchPhase.Began)
    8.             //if (Input.GetButtonDown("Fire1"))
    9.             {  
    10.                 // var touchPos : Vector3 = Input.GetTouch(i).position;
    11.  
    12.                  var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);
    13.                  var hit : RaycastHit;
    14.  
    15.                     if (Physics.Raycast (ray, hit)){
    16.                             move();
    17.                            
    18.                             //Destroy(hit.rigidbody);
    19.                         }
    20.             }
    21.         }
    22.     }
    23. function move(){
    24.      //var clone:Rigidbody;
    25.      //clone=Instantiate(ball, transform.position,Quaternion.identity);
    26.      ball.useGravity=false;
    27.      ball.AddForce(transform.TransformDirection(0.0f,10.0f,0.0f)*-20);
    28.      ball.transform.parent=null;
    29.      //Destroy(ball.gameObject,5);
    30.      Debug.Log("button");
    31. }
     
    Last edited: May 19, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you're not checking what the RaycastHit actually hit... do you want the user to tap on each specific ball or just anywhere on the screen?
     
  3. gokulreddy

    gokulreddy

    Joined:
    Apr 2, 2014
    Posts:
    11
    I need user to tap on blue sprite so that ball attached to it should detach from it
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    "the blue sprite"... try to remember we can't see your project so that isn't the most helpful description... is the "other ball" on a different colour sprite?

    the above script is attached to the (assume there are several) sprite gameobjects?
     
  5. gokulreddy

    gokulreddy

    Joined:
    Apr 2, 2014
    Posts:
    11
  6. gokulreddy

    gokulreddy

    Joined:
    Apr 2, 2014
    Posts:
    11
    when i hit on blue sprite small ball should detach from the blue sprite
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ah ok, so the blue sprite is the link between the big ball and the little ball... gotcha :)

    you need to check if the "hit.transform" is the "ball.transform.parent". The blue sprite would need to have a collider for the raycast to hit it etc. :)
     
  8. gokulreddy

    gokulreddy

    Joined:
    Apr 2, 2014
    Posts:
    11
    yea i have inserted box collider to both the blue sprites but when i hit on any one blue sprite both small balls are detaching from it.
     
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    have you updated the code above to compare the hit.transform with the ball.transform.parent?
     
  10. gokulreddy

    gokulreddy

    Joined:
    Apr 2, 2014
    Posts:
    11
    where to update with ball.transform.parent can you explain me
     
  11. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    currently you call "Move()" when the touch hits anything
    Code (csharp):
    1.  
    2. if (Physics.Raycast (ray, hit))
    3. {
    4.     move();
    5. }
    6.  

    but you only want the script to do something if the touch was over a specific thing in your scene, so you need to check for that
    Code (csharp):
    1.  
    2. if (Physics.Raycast (ray, hit))
    3. {
    4.     if(hit.transform == ball.transform.parent)
    5.     {
    6.         move();
    7.     }
    8. }
    9.  
     
  12. gokulreddy

    gokulreddy

    Joined:
    Apr 2, 2014
    Posts:
    11
    Thanks a lot its working:):):)
     
  13. gokulreddy

    gokulreddy

    Joined:
    Apr 2, 2014
    Posts:
    11
    Finally stuck with one more issue,After detaching a ball a new should come n attach to blue sprite with small delay.
     
  14. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148