Search Unity

How to Get Player Off of Swing He is Childed To

Discussion in 'Scripting' started by artistshc, Aug 20, 2015.

  1. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    Hi there. I have a swing that you drag and let go and it swings back and forth. I then drop the player on it and he swings back and forth with the swing. The problem is getting the player off because when I drag the player off of the swing, the swing also is pulling along with the player...so it is really hard to get him off of the swing. Any ideas?

    Here is the code that is attached to the player

    Code (CSharp):
    1. void OnMouseDown(){
    2.         transform.parent = null;
    3.         transform.rotation = Quaternion.identity;  
    4.         animator.SetTrigger ("Stand");
    5.         isChilded = false;
    6.         isClicked = true;
    7.     }
    8.     void OnMouseUp(){
    9.         isClicked = false;
    10.     }
    11.    
    12.     // Use this for initialization
    13.     void Start () {
    14.         animator = GetComponent<Animator>();
    15.         isChilded = false;
    16.         isClicked = false;
    17.     }
    18.  
    19.  
    20.  
    21.    
    22.     //Drag and Drop Character
    23.     void OnMouseDrag()
    24.     {
    25.         Vector3 point = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,(transform.position.z-Camera.main.transform.position.z)));
    26.         point.z = transform.position.z;
    27.         transform.position = point;
    28.  
    29.     }
    30.  
    31.     //CHARACTER SITS
    32.     void OnTriggerEnter2D(Collider2D other) {
    33.  
    34.         if (other.gameObject.tag == "Sit") {
    35.             //set the animation trigger bool Sit to true to start animation
    36.             animator.SetTrigger ("Sit");
    37.  
    38.         }
    39.  
    40.         //if comes in contact with swing then...
    41.         else if (other.gameObject.name == "Swing") {
    42.             isChilded = true;
    43.             //set the animation trigger bool Sit to true to start animation
    44.             if(isClicked){
    45.                 animator.SetTrigger ("Sit");
    46.                 gameObject.transform.parent = other.transform;
    47.                 gameObject.transform.rotation = other.transform.rotation;
    48.             }
    49.         }
    50.  
    51.  
    52.  
    53.     }
    54.  
    55.  
    56.    
    57.     //CHARACTER STANDS
    58.     void OnTriggerExit2D (Collider2D other) {
    59.         if (other.gameObject.tag == "Pickable") {
    60.             //set the animation trigger bool Sit to true to start animation
    61.             animator.SetTrigger ("Stand");
    62.             //set the animation trigger bool atMouth to true to start animation
    63.         }
    64.  
    65.         //if comes in contact with swing then...
    66.         else if (other.gameObject.name == "Swing") {
    67.             //set the animation trigger bool Sit to true to start animation
    68.             animator.SetTrigger ("Stand");
    69.             //other.transform.parent = this.transform;
    70.  
    71.  
    72.         } else {
    73.             transform.parent = null;
    74.             transform.rotation = Quaternion.identity;  
    75.             isChilded = false;
    76.         }
    77.     }
    Please let me know how you would go about getting the player off of the swing. Thanks!