Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Hyppetrain · Oct 08, 2021 at 06:53 PM · tilemapnewbiebuildingrtsplacement

using Tilemap for 3D RTS game

Hello, I want to try to make my first RTS game and wanted to ask if Tilemaps can be used as a grid for placing buildings in real time (meaning, when I click a building from UI selection, the tilemap shows up and after I place the building it snaps to the tile) or do I have to create my own grid system for this?

thanks a bunch

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by andrew-lukasik · Oct 09, 2021 at 10:53 PM

Create your own little grid system, it's not that complicated

grid system example

LetsBuildAGrid.cs

 using System.Collections.Generic;
 using UnityEngine;
 public class LetsBuildAGrid : MonoBehaviour
 {
 
     [SerializeField] Vector3 _gridOrigin = Vector3.zero;
     [SerializeField] Vector2 _gridCellSize = new Vector2( 3f , 3f );
     [SerializeField] Camera _camera = null;
     Vector2Int _cursorCoord;
 
     [SerializeField] Mesh _gridCellMesh = null;
     [SerializeField] Vector3 _gridCellMeshScale = Vector3.one;
     [SerializeField] Vector3 _gridCellMeshRotation = Vector3.zero;
     [SerializeField] Material _gridCellMaterial = null;
     Matrix4x4[] _gridCellsToRender = new Matrix4x4[1];
     
     [SerializeField] GameObject _prefabToSpawn = null;
     Dictionary<Vector2Int,GameObject> _prefabInstances = new Dictionary<Vector2Int,GameObject>();
 
     void Update ()
     {
         var gridPlane = new Plane( Vector3.up , _gridOrigin );
         var ray = _camera.ScreenPointToRay( Input.mousePosition );
         if( gridPlane.Raycast( ray , out float rayHitDist ) )
         {
             Vector3 cursorPos = ray.origin + ray.direction*rayHitDist;
             Vector2Int cursorGridCoord = new Vector2Int(
                 Mathf.FloorToInt( cursorPos.x/_gridCellSize.x ) ,
                 Mathf.FloorToInt( cursorPos.z/_gridCellSize.y )
             );
             _cursorCoord = cursorGridCoord;
         }
         Vector3 gridCellCenter = CoordToCenter( _cursorCoord );
         
         _gridCellMaterial.SetPass(0);
         _gridCellsToRender[0] = Matrix4x4.TRS( gridCellCenter , Quaternion.Euler(_gridCellMeshRotation) , _gridCellMeshScale );
         Graphics.DrawMeshInstanced( _gridCellMesh , 0 , _gridCellMaterial , _gridCellsToRender );
 
         if( Input.GetMouseButtonDown(0) )
         if( !_prefabInstances.ContainsKey(_cursorCoord) )
         {
             var instance = GameObject.Instantiate( _prefabToSpawn , gridCellCenter , Quaternion.identity );
             _prefabInstances.Add( _cursorCoord , instance );
         }
     }
 
     #if UNITY_EDITOR
     void OnDrawGizmos ()
     {
         Gizmos.DrawWireCube( CoordToCenter(_cursorCoord) , new Vector3( _gridCellSize.x , 0 , _gridCellSize.y ) );
     }
     #endif
 
     Vector3 CoordToCorner ( Vector2Int coord ) => new Vector3( coord.x*_gridCellSize.x , _gridOrigin.y , coord.y*_gridCellSize.y );
     Vector3 CoordToCenter ( Vector2Int coord ) => CoordToCorner(coord) + new Vector3( _gridCellSize.x , 0 , _gridCellSize.y )*0.5f;
 
 }


w5uhiugehw390tewfujhhoh3wr908iu.gif (158.9 kB)
Comment
Add comment · Show 1 · 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
avatar image Hyppetrain · Oct 10, 2021 at 02:58 AM 0
Share

Yep I already did, but thanks for your effort.

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

134 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

Related Questions

RTS building placement collision problem C# 1 Answer

RTS Building Structures - How to have building follow my cursor until I click and place it on the grounds? 2 Answers

RTS Style building help 2 Answers

How can I make this move in 3d space?[BUILDING SYSTEM] 1 Answer

Best solution for Grid-based building system? 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