I created a grid-based block placement system with mouse input like this video:
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;
}
}`