Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Goodgamejuli · May 20, 2020 at 07:00 PM · touchgridtouch controlsgrid based gameblock

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;
     }
 }`


Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image blinkafrootable · May 20, 2020 at 09:28 PM 0
Share

Could you please reformat the code using the Code Sample text formatting? It's super hard to read the code as it is.

avatar image Goodgamejuli · May 20, 2020 at 11:32 PM 0
Share

Of course, sorry. ^^

avatar image Goodgamejuli · May 22, 2020 at 10:52 AM 0
Share

Any ideas?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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();
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

163 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to get intermediate swipe positions? 1 Answer

How to get multitouch input using EasyTouch 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges