Search Unity

Distance Joint 2D Attachment

Discussion in 'Physics' started by Ironlors, Dec 28, 2015.

  1. Ironlors

    Ironlors

    Joined:
    Dec 28, 2015
    Posts:
    1
    Hi,
    I would like to ask how I can add an Connected Ridgidbody 2D with a script.

    1st I want to explain my situation.
    I am working on a stacking game where you are dropping blocks in order to stack them.

    The upper block is attached to an anchor with a distance joint 2D.
    In order to drop the old distance joint 2d is getting destroyed.
    At the moment the block hits the ground another block is getting spawned.
    The anchor and the camera are going to move upwards and the score is getting up.

    Problem:
    The blocks are getting spawned, and they are falling down, triggering other spawns, but the distance joint is not attached to the anchor so they are just falling down.

    Here are the hits, where HIT is the script attached to the blocks.
    I think the problem could be solved, if I would attach the anchor at the moment the box is spawned.

    I am pretty new to game development, so I would be happy with every advise I could get.
    I attached the zipped project files.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Hit : MonoBehaviour {
    5.  
    6.     public Transform Box;
    7.     public Transform Anchor;
    8.     public float y;
    9.  
    10.     void OnTriggerEnter2D(Collider2D other)
    11.     {
    12.         if (other.gameObject.CompareTag("Block")) {
    13.             hit ();
    14.         }
    15.     }
    16.  
    17.     void Update(){
    18.     }
    19.  
    20.     void hit(){
    21.         Debug.Log ("BLOCK");
    22.         gameObject.tag = "used";
    23.         GameObject.Find ("CameraRef").transform.Translate (0f, 0.7f, 0f);
    24.         GameObject.Find ("Anchor").transform.Translate (0f, 0.7f, 0f);
    25.         Score.score = Score.score + 10;
    26.         spawn ();
    27.     }
    28.  
    29.     void spawn(){
    30.         y = GameObject.Find ("Anchor").transform.position.y;
    31.         GameObject.Find ("Anchor").transform.Translate (0f, 1, 0f);
    32.         Instantiate (Box,new Vector3 (0f, y-1, 0f), Quaternion.identity);
    33.     }
    34. }
    35.  
     

    Attached Files: