Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Help for newbie

Discussion in '2D' started by djakus, Nov 10, 2019.

  1. djakus

    djakus

    Joined:
    Nov 10, 2019
    Posts:
    3
    Hi there!

    I'm a total newbie regarding unity engine. I've done some small tutorials.

    Now im trying to create my own "Alien Invaders" clone. I think i got most of the stuff i need to do regarding my game but I've got stuck with some pretty stupid thing.

    I've got 3 game objects. Bullet, BlockEraser and BaseBlock. When I hit BaseBlock with Bullet i want to spawn BlockEraser.

    This way eventually my BaseBlock gets destroyed and Bullets should fly through BlockEraser but should get destroyed on BaseBlock collision.

    Image with black background is what i want to achieve.

    What i did currently is that I added rigid body to Bullet, box colliders for all of the 3 game objects and checked Bullet isTrigger.

    I attached the script for Bullet object and added this code in it:

    private GameObject eraser;

    void Start()

    {

    eraser = GameObject.FindGameObjectWithTag("EraserBlock");

    }

    void Update()

    {

    Vector3 temp = gameObject.transform.position;

    temp.x += 1f*Time.deltaTime;



    gameObject.transform.position = temp;

    }

    private void OnTriggerEnter2D(Collider2D hitobject)

    {

    if (hitobject.tag == "BaseShield")

    {

    if (hitobject.tag != "EraserBlock")

    {

    CreateEraserBlock(gameObject);

    }

    }

    }

    void CreateEraserBlock(GameObject gameObject)

    {

    Instantiate(eraser, gameObject.transform.position, Quaternion.identity);

    Destroy(gameObject);

    }

    My problem arises when the Bullet hits the BaseBlock.

    Everytime it spawns EraserBlock at the same location (image with blue background provided just to showcase the problem), instead it should progressively create a hole(tunnel) through BaseBlock.

    Any help would be appreciated.



    Thnx
     

    Attached Files:

  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,147
    use code tags for better readable. And I am not sure, that the script will run from the first try :)
    Code (CSharp):
    1.     private GameObject eraser;
    2.     //I think here you should use prefab from the eraser
    3.     //public GameObject eraser;
    4.    
    5.     //here you can change the speed from the bullet
    6.     //public float speed;
    7.  
    8.     void Start()
    9.     {
    10.         eraser = GameObject.FindGameObjectWithTag("EraserBlock");
    11.         //as said, maybe prefab, then no need to look for it
    12.     }
    13.  
    14.     void Update()
    15.  
    16.     {
    17.         Vector3 temp = gameObject.transform.position;
    18.         //adjust the speed from the editor
    19.         //temp.x += speed * Time.deltaTime;
    20.         temp.x += 1f * Time.deltaTime;
    21.         gameObject.transform.position = temp;
    22.     }
    23.  
    24.     private void OnTriggerEnter2D(Collider2D hitobject)
    25.  
    26.     {
    27.         if (hitobject.tag == "BaseShield")
    28.         {
    29.             //I see no use in this second check, the tag here is already other as EraserBlock (== Baseshield)
    30.             if (hitobject.tag != "EraserBlock")
    31.             {
    32.                 //CreateEraserBlock(gameObject);
    33.                 //and I think you will destroy the green block and the bullet?
    34.                 CreateEraserBlock(hitobject.gameObject);
    35.  
    36.             }
    37.         }
    38.     }
    39.  
    40.     //void CreateEraserBlock(GameObject gameObject)
    41.     //use here an other word as parameter, gameObject is by default this gameobject
    42.     void CreateEraserBlock(GameObject hittedObject)
    43.     {
    44.         //delete green block
    45.         Destroy(hittedObject);
    46.         //spawn the eraser on the place from the destroyed block
    47.         Instantiate(eraser, hittedObject.transform.position, Quaternion.identity);
    48.         //was this object bullet ? here you can delete it
    49.         Destroy(gameObject);
    50.     }
     
    Last edited: Nov 10, 2019
  3. djakus

    djakus

    Joined:
    Nov 10, 2019
    Posts:
    3
    Thnx for trying to help me.. Problem is not destroying the whole green object(base).. I want to destroy only part of it in the way i spawn black object over the green one.. That way i want to destroy it piece by piece. I dont know how to achieve my intention.

    Thnx
     
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,147
    I thought, the base was builded from the many small green blocks :rolleyes:
    Then this would be working like here:


    you can try to make the base without collider. Place the erasers over the base with enabled collider but disabled sprite renderer. Then, if the collider was hitted, disable collider and enable sprite renderer.

    other way, I think, you should change the texture and poligon collider from the base. Sorry, I have never did it.
    You can try to google: destructible 2d terrain like worms.
     
    Last edited: Nov 10, 2019
    djakus likes this.
  5. djakus

    djakus

    Joined:
    Nov 10, 2019
    Posts:
    3
    Thnx a lot.