- Home /
 
grid-based block placement system with touch input
I created a grid-based block placement system with mouse input like this video: https://youtu.be/0WZUgUtBxcg?t=1982
At the moment I can only place blocks with a click of the mouse at the point I am looking at. Now I want to place the blocks with touch input on mobile devices. I am new to Unity so can someone give me hints or even better adjust the code right away? Here is the code:
 `using System.Collections;  
 using System.Collections.Generic;  
 using UnityEngine;  
 public class BuildingSystem : MonoBehaviour  
 
 {
 
     [SerializeField]
     private Camera playerCamera;
 
     private bool buildModeOn = true;
     private bool canBuild = true;
 
     private BlockSystem bSys;
 
     [SerializeField]
     private LayerMask buildableSurfacesLayer;
 
     private Vector3 buildPos;
 
     private GameObject currentTemplateBlock;
 
     [SerializeField]
     private GameObject blockTemplatePrefab;
     [SerializeField]
     private GameObject blockPrefab;
 
     [SerializeField]
     private Material templateMaterial;
 
     private int blockSelectCounter = 0;
 
     private void Start()
     {
         bSys = GetComponent<BlockSystem>();
     }
 
     private void Update()
     {
         if (Input.GetKeyDown("e"))
         {
             buildModeOn = !buildModeOn;
 
             if (buildModeOn)
             {
                 Cursor.lockState = CursorLockMode.Locked;
             }
             else
             {
                 Cursor.lockState = CursorLockMode.None;
             }
         }
 
         if (Input.GetKeyDown("r"))
         {
             blockSelectCounter++;
             if (blockSelectCounter >= bSys.allBlocks.Count) blockSelectCounter = 0;
         }
 
         if (buildModeOn)
         {
             RaycastHit buildPosHit;
 
             if (Physics.Raycast(playerCamera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0)), out buildPosHit, 10, buildableSurfacesLayer))
             {
                 Vector3 point = buildPosHit.point;
                 buildPos = new Vector3(Mathf.Round(point.x), Mathf.Round(point.y), Mathf.Round(point.z));
                 canBuild = true;
             }
             else
             {
                 Destroy(currentTemplateBlock.gameObject);
                 canBuild = false;
             }
         }
 
         if (!buildModeOn && currentTemplateBlock != null)
         {
             Destroy(currentTemplateBlock.gameObject);
             canBuild = false;
         }
 
         if (canBuild && currentTemplateBlock == null)
         {
             currentTemplateBlock = Instantiate(blockTemplatePrefab, buildPos, Quaternion.identity);
             currentTemplateBlock.GetComponent<MeshRenderer>().material = templateMaterial;
         }
 
         if (canBuild && currentTemplateBlock != null)
         {
             currentTemplateBlock.transform.position = buildPos;
 
             if (Input.GetMouseButtonDown(0))
             {
                 PlaceBlock();
             }
         }
     }
 
     private void PlaceBlock()
     {
         GameObject newBlock = Instantiate(blockPrefab, buildPos, Quaternion.identity);
         Block tempBlock = bSys.allBlocks[blockSelectCounter];
         newBlock.name = tempBlock.blockName + "-Block";
         newBlock.GetComponent<MeshRenderer>().material = tempBlock.blockMaterial;
     }
 }`
 
              Could you please reformat the code using the Code Sample text formatting? It's super hard to read the code as it is.
Answer by blinkafrootable · May 24, 2020 at 10:16 PM
Unity's touchscreen input system works a little differently from its normal mouse and keyboard input system, and you'll have to fine-tune the way it works to your liking. A simple way of replacing a mouse click with the tap of a finger would be like this:
 if (Input.touchCount > 0) // if at least one finger is touching the screen
 {
     if (Input.touches[0].phase == TouchPhase.Began) // if the first finger (ignoring the rest) just touched the screen
     {
         PlaceBlock();
     }
 }
 
              Your answer
 
             Follow this Question
Related Questions
store the user touch input 0 Answers
Block puzzle (1010) help 0 Answers
Find a path with specific steps (moves) on a grid 0 Answers