Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Minecraft like, destroying block script

Discussion in '2D' started by Syriders, Jul 2, 2014.

  1. Syriders

    Syriders

    Joined:
    Jul 2, 2014
    Posts:
    1
    Hello guys,

    I would like a script that allows me to destroy a block several times in order to create a minecraft like. I mean the script that allows my character to destroy a nearby block as in minecraft (in several shots then). I am a beginner in programming and I do not have enough time to find how to make this script (although I think it is relatively easy to do).

    Thank you for your understanding Sincerely Syriders.
     
  2. Whiteleaf

    Whiteleaf

    Joined:
    Jul 1, 2014
    Posts:
    728
    Did you even look for any? And if you don't have the time to find a tutorial, then don't you not have time to make your game? You can't make a game without coding it. And no one is going to just make the game for you.
     
    Rutenis likes this.
  3. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    "I am a beginner in programming and I do not have enough time to find how to make this script (although I think it is relatively easy to do)."

    Seriously?
     
    koxta likes this.
  4. Whiteleaf

    Whiteleaf

    Joined:
    Jul 1, 2014
    Posts:
    728
    ^^^^^
     
  5. Kilrath81

    Kilrath81

    Joined:
    Nov 19, 2013
    Posts:
    153
  6. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    I'm sure you also plan to make Minecraft with an individual GameObject for each block too?
     
    Rutenis likes this.
  7. Hazem101

    Hazem101

    Joined:
    Nov 13, 2014
    Posts:
    2
    guys stop fighting i really need help with minecraft breaking block script and i have an excuse for not knowing it:
    i am 11 years old i am in middle school i have a lot of work i am just a beginer plzzz just help please
     
    MilanCREEPERPOWER likes this.
  8. Hazem101

    Hazem101

    Joined:
    Nov 13, 2014
    Posts:
    2

    New Member
    Joined:
    Today
    Messages:
    1
    guys stop fighting i really need help with minecraft breaking block script and i have an excuse for not knowing it:
    i am 11 years old i am in middle school i have a lot of work i am just a beginer plzzz just help please
     
  9. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256
    You probably want to read this gigantic 52 page thread on Minecraft remakes in Unity:
    http://forum.unity3d.com/threads/after-playing-minecraft.63149/

    Also here's a Unity project which creates entire Minecraft-like maps:
    https://github.com/chraft/chunk-light-tester/
     
  10. Wrymnn

    Wrymnn

    Joined:
    Sep 24, 2014
    Posts:
    352
    I will write you a script to destroy nearby block in 2D side view.

    Place this script into your block:
    (I have done my part, now you have to do yours.)
    1. Change the TAG of your player to "Player"
    2. Add BoxCollider2D to your blocks
    3. Create script and put this code into the script.
    4. Attach the script to your block object.

    Code (csharp):
    1.  
    2. public int timeToMine; //Tme it takes to mine 1 Health out of block
    3. public int blockHealth; //Health of block
    4. public int maxMiningDistance;  //How far can player stand from block so he can mine
    5.  
    6.  
    7. private GameObject player; //You either attach player object here via Inspector panel, or find Player by tag
    8. private float counter;
    9.  
    10.  
    11. void Start()
    12. {
    13.     //We find Player object
    14.     player = GameObject.FindGameObjectWithTag("Player"); //Note you have to change Player`s tag to "Player" or it won`t find him.
    15. }
    16. //So if mouse is over the block
    17. void OnMouseEnter()
    18. {
    19.     //We have to get distance between player and the current block
    20.     Vector2 distance = player.transform.position - transform.position;
    21.  
    22.     if(Input.GetButton(0) && distance.magnitude < maxMiningDistance) //If we hold left mouse button AND we are close enough to block
    23.     {
    24.         counter += Time.deltaTime;
    25.      
    26.         if(counter >= timeToMine) //If we mined given time, subtrack 1 from block health
    27.         {
    28.             blockHealth -= 1;
    29.             counter = 0;
    30.             //Here you can also change sprite of the given block like in Minecraft (So it looks cracked)
    31.          
    32.             //If block has less than 0 health, destroy it
    33.             if(blockHealth < 0)
    34.                 Destroy(gameObject;)
    35.                 //also you can add here that mined block will go straight to your inventory
    36.         }
    37.     }
    38. }
    39.  
    Now you just change the values of your block prefab as you want. Everything else is taken care of.
    You can create different type of Blocks like Iron, or adamantium and change their Health so they are more durrable like in minecraft.

    If you need any further help, just post it here :D

    Enjoy!
     
    Last edited: Nov 14, 2014
    GrinGoGreat likes this.