Search Unity

Looking For A Block Based In-game Editor Tutorial

Discussion in '2D' started by EstragonHelmer, Apr 15, 2019.

  1. EstragonHelmer

    EstragonHelmer

    Joined:
    Oct 26, 2017
    Posts:
    12
    Hard to explain what I'm after in just a title, but my Googling hasn't found anything quite like what I need. I'm looking for a tutorial on how to make an in-game editor that uses blocks on a grid, where the blocks kind of magnetize to each other, so you can't just build anywhere, it all has to connect up.

    This is something I made years ago in Game Maker Studio, so the code is really messy and not worth unpicking as it's in GML. It was made very early in my learning of game development, so the code is really bad anyway.

    There's a .gif here - https://twitter.com/EstragonHelmer/status/858451285485518848

    But it's a good example of what I'm looking for. I'm not entirely sure how to search for that because all I kept getting were tutorials on how to make stuff snap to the grid in Unity's editor, rather than in-game.

    Does anyone know any good tutorials for something similar to this?
     
  2. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    Not sure about tutorials but i'm working on something similar in 3d. Maybe you will get some inspiration.
    Code (CSharp):
    1. void Clicked()
    2.     {
    3.         var ray = cam.ScreenPointToRay(Input.mousePosition);
    4.  
    5.         RaycastHit hit = new RaycastHit();
    6.        
    7.  
    8.         if (Physics.Raycast(ray, out hit))
    9.         {
    10.             MCFace face = GetHitFace(hit);
    11.             Vector3 offset = GetPlacementPos(face);
    12.  
    13.             if(offset != Vector3.zero){
    14.                 GameObject c = Instantiate(cube);
    15.                 c.transform.position = offset + hit.rigidbody.transform.position;
    16.             }
    17.  
    18.            
    19.         }
    20.        
    21.      
    22.     }
    23.     public enum MCFace
    24.     {
    25.         None,
    26.         Up,
    27.         Down,
    28.         East,
    29.         West,
    30.         North,
    31.         South
    32.     }
    33.  
    34.     public MCFace GetHitFace(RaycastHit hit)
    35.     {
    36.         Vector3 incomingVec = hit.normal - Vector3.up;
    37.  
    38.         if (incomingVec == new Vector3(0, -1, -1))
    39.             return MCFace.South;
    40.  
    41.         if (incomingVec == new Vector3(0, -1, 1))
    42.             return MCFace.North;
    43.  
    44.         if (incomingVec == new Vector3(0, 0, 0))
    45.             return MCFace.Up;
    46.  
    47.         if (incomingVec == new Vector3(1, 1, 1))
    48.             return MCFace.Down;
    49.  
    50.         if (incomingVec == new Vector3(-1, -1, 0))
    51.             return MCFace.West;
    52.  
    53.         if (incomingVec == new Vector3(1, -1, 0))
    54.             return MCFace.East;
    55.  
    56.         return MCFace.None;
    57.     }
    58.  
    59.     private Vector3 GetPlacementPos(MCFace dir){
    60.  
    61.         if (dir == MCFace.None){ return Vector3.zero; }
    62.         if (dir == MCFace.Up){ return Vector3.up; }
    63.         if (dir == MCFace.Down) { return Vector3.down; }
    64.         if (dir == MCFace.North) { return Vector3.forward; }
    65.         if (dir == MCFace.East) { return Vector3.right; }
    66.         if (dir == MCFace.South) { return Vector3.back; }
    67.         if (dir == MCFace.West) { return Vector3.left; }
    68.         else{ return Vector3.zero; }
    69.     }
     
    EstragonHelmer likes this.
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You can use Unity's Grid component to create the grid and its functions to snap things to it.

    Anytime you want something to snap to the grid you can convert its world position to the cell it is in, and then find that cells world position to set your object to.

    Check out the example code for `GridLayout.WorldToCell`:
    https://docs.unity3d.com/ScriptReference/GridLayout.WorldToCell.html

    Based on your link though, you probably want to run this snapping on Update, or during a click & drag.
     
    EstragonHelmer likes this.