Search Unity

how to create a block placement system like in Solomon's Key

Discussion in '2D' started by ployer, Mar 24, 2017.

  1. ployer

    ployer

    Joined:
    Jul 28, 2009
    Posts:
    41
    The easiest way to explain is to show you a video of it being used in a old NES game



    Basically i want to create a grid system that when the players hits a button it creates ablock in the closest grid position. This is for a 2d platformer.
     
    theANMATOR2b likes this.
  2. webox

    webox

    Joined:
    Aug 27, 2015
    Posts:
    72
    Hey man! I've been working on a prototype for your problem, it can serve you as a starting point maybe!
    It looks like this



    Controllers:
    • Left Arrow to go left
    • Right arrow to go right
    • Up arrow to jump
    • Space to place blocks
    It's a basic character controller with a "magic stick" attached as a child object and a "block dispenser". The block dispensers is in charge of instantiating the blocks in the correct position. All the sprites are set to 128 px per unit so I can easily place the tiles at absolute positions. If the block dispenser is at x=1.34 y=1.46 the dispenser rounds up to 1,1 and places the object in that position.

    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.Space))
    2. {
    3.             if(hit.collider == null)
    4.                 if (block){
    5.                     Vector2 position = new Vector2(Mathf.RoundToInt(gameObject.transform.position.x), Mathf.RoundToInt(gameObject.transform.position.y));
    6.                     Instantiate(block, position, Quaternion.identity);
    7.                 }
    8. }

    Since every tile is 128x128 there's no overlapping adn every tile fits perfectly


    I used kenney assets, you can clone it from my github and use it however you want.

    Hope this helps!
     
  3. ployer

    ployer

    Joined:
    Jul 28, 2009
    Posts:
    41
    thats fantastic, thank you!