Search Unity

Attach 2D character to the rope

Discussion in 'Scripting' started by AMIR_REZAs, Aug 19, 2015.

  1. AMIR_REZAs

    AMIR_REZAs

    Joined:
    Jan 20, 2014
    Posts:
    54
    Hello
    I have a problem in scripting player attach the rope(grab it)
    I want that my character always has specific distance to the rope even when my 2d rope has swing.
    With the codes below the character doesn't the distance any time!
    please help me about this.



    Code (CSharp):
    1.        
    2. void OnTriggerEnter2D(Collider2D col){
    3.  
    4.             if (col.tag == "Rope") {
    5.  
    6.                 if(!isRopeMode){
    7.                 isRopeMode = true;
    8.                 tempRope = col.gameObject;
    9.                 GetComponent<Rigidbody2D> ().gravityScale = 0;
    10.                 GetComponent<Rigidbody2D> ().isKinematic = true;
    11.                 }else{
    12.  
    13.  
    14.                 }
    15.  
    16.             }
    17.         }
    18.        private void FixedUpdate()
    19.         {
    20.  
    21.             if (isRopeMode) {
    22.                 transform.position = new Vector2 (tempRope.transform.position.x - 0.2f, transform.position.y);
    23.                 transform.rotation = tempRope.gameObject.transform.GetChild(0).transform.rotation;
    24.             }
    25. }
    26.  
     
  2. sz-Bit-Barons

    sz-Bit-Barons

    Joined:
    Nov 12, 2013
    Posts:
    150
    How did you implement the rope?
     
  3. AMIR_REZAs

    AMIR_REZAs

    Joined:
    Jan 20, 2014
    Posts:
    54
    By Hinge Joint and 2D features