Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 jpthek9 · Jan 16, 2015 at 12:06 AM · gridbuildingmodular

Dynamic 2D Grid Building

How would I have building placement on a grid located on the deck of i.e. a ship. This ship can turn and move around, but the grid will always stay the same relative to the ship. Then, turrets or barracks can be built on this ship's grid and those buildings can be anywhere from 1x1 to 5x5. With the addition of multiple ships, this problem rattles my brain even more.

Are there any clean solutions to accomplish this? or are there any assets that will help me implement this into my game?

Comment
Add comment · Show 12
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 DanSuperGP · Jan 16, 2015 at 12:10 AM 0
Share

Checkout "Grids by GameLogic" It's a really fantastic asset for grids that will vastly simplify what you're trying to do.

Even using that asset, this will require quite a bit of custom code to make the grids work in the manner you want. It's no small task.

Still, it will be much easier than trying to make it from scratch.

avatar image jpthek9 · Jan 16, 2015 at 12:13 AM 0
Share

Oh, thanks! I'd rather not break the bank until I know for sure it's the product I need.

I'm okay with making it from scratch. Just need some help brainstor$$anonymous$$g solutions. I was thinking of doing something with colliders but I'd much rather keep it all in code or it could get very messy.

avatar image DanSuperGP · Jan 16, 2015 at 12:20 AM 0
Share

It's a pretty robust product, and yes, you can create grids dynamically from script or with an editor.

One of the really cool things about it is the grids have two components, an efficient datastructure component, and a map component that controls how the data structure is mapped into world space.

What you're going to have to do to make it work is write a custom $$anonymous$$ap for your grid that will map the space relative to your ships, which can move around. It shouldn't be too bad.

If you are only doing Rectangle grids, there is a $20 version with just rect, and a $50 option with just Rect and Hex I bought the $100 version because I like having all the options but it's nice to be able to save money if you don't need all the weird grids.

I've used it on multiple projects, and I can attest to it's greatness.

avatar image DanSuperGP · Jan 16, 2015 at 12:22 AM 0
Share

I would recommend checking out their videos and forums also.

avatar image DanSuperGP · Jan 16, 2015 at 12:57 AM 1
Share

Well, the way to deal with the rotation is to get the location pointed at in world space, and then translate it into the ship's localspace using http://docs.unity3d.com/ScriptReference/Transform.InverseTransformPoint.html

So then if your mast is (0,0,0) and your prow is (0,0,10) and you click on the prow, the inverseTransformPoint will give you (0,0,10) no matter what the rotation or position of the ship is.

Show more comments

1 Reply

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

Answer by DanSuperGP · Jan 16, 2015 at 01:05 AM

Well, the way to deal with the rotation is to get the location pointed at in world space, and then translate it into the ship's localspace using http://docs.unity3d.com/ScriptReference/Transform.InverseTransformPoint.html

So then if your mast is (0,0,0) and your prow is (0,0,10) and you click on the prow, the inverseTransformPoint will give you (0,0,10) no matter what the rotation or position of the ship is.

(Posted as an answer at the request of OP)

Comment
Add comment · Show 2 · 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 jpthek9 · Jan 16, 2015 at 01:07 AM 1
Share

Awesome! From here, it's just implementing a simple 2D grid system with rounding for snap and matching the position returned to the grid's data system.

avatar image jpthek9 · Jan 16, 2015 at 05:12 AM 0
Share

I just implemented the dynamic building system into my game and it worked beyond awesomely. Here's some of my code in case anyone wants to see how it works.

 void Do$$anonymous$$ouseOver(){ //Only works if the mouse is over the grid; this is triggered by a bool in Update
             RaycastHit hit;
         float CastDist = (Camera.main.transform.position-(transform.position)).magnitude + (float)xLength/2 + (float)yLength/2;
             if (Physics.Raycast(Camera.main.ScreenPointToRay (Input.mousePosition), out hit,CastDist, CastLayer))
             {
             CurX = $$anonymous$$athf.FloorToInt(transform.InverseTransformPoint(hit.point).x * xLength / 10);
             CurX += xLength / 2;
             CurY = $$anonymous$$athf.FloorToInt(transform.InverseTransformPoint(hit.point).z * yLength / 10);
             CurY += yLength / 2;
             print (CurX + "," + CurY);
             }
     }

Where xLength and yLength are the corresponding sizes of the grid.

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

26 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

Related Questions

Grid system visualization 1 Answer

Minecraft like building? 2 Answers

Why can't I build Web Player in Unity while I have no problems with building standalone versions? 2 Answers

Unity Wall placement help 0 Answers

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