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 smirlianos · Apr 14, 2014 at 10:03 AM · 2dinstantiategrid

2D grid for building

Hello,

I try to make a 2D building game. The part of the code is supposed to Instantiate a cube wherever the player clicks. But it must be aligned as a grid (2D minecraft).

The problem is that the "Mathf.FloorToInt" I use converts for example the float "0,2" -> "0", so the Cube is instantiated one unit to the left, not on the part of the grid the user clicks.

Any help?

 #pragma strict
 var block : GameObject;
 
 
 function Update () {
     var pos : Vector3 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     var posIntX : int =  Mathf.FloorToInt(pos.x);
     var posIntY : int = Mathf.FloorToInt(pos.y);
     if(Input.GetMouseButtonDown(0))
     {
         Instantiate(block,Vector3(posIntX,posIntY,0),transform.rotation);
     }
 }
Comment
Add comment · Show 4
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 GenuiTix · Apr 14, 2014 at 10:39 AM 1
Share

If the X axis is the problem then:

  • you can use ceiling ins$$anonymous$$d of flooring (not sure that such function exists)

  • or the easy one:

    var posIntX : int = $$anonymous$$athf.FloorToInt(pos.x) + 1;

avatar image smirlianos · Apr 14, 2014 at 10:58 AM 0
Share

The problem is on both axis, if I click < .5 it rounds it to 0. if I click > .5 it rounds it to 1

avatar image AlucardJay · Apr 14, 2014 at 11:28 AM 1
Share

this depends on the pivot point of your block. I assume you are using a standard cube which has its pivot in the center. If you made the cube a child of an empty gameObject, and position it locally to (0.5,0.5,0.5) then the cube would sit where you want it.

alternatively, you could modify your equation to account for the pivot of the cube :

 var posIntX : int =  $$anonymous$$athf.FloorToInt(pos.x+0.5);
 var posIntY : int = $$anonymous$$athf.FloorToInt(pos+0.5);
 //
 Instantiate(block,Vector3(posIntX,posIntY,0.5),transform.rotation);
avatar image smirlianos · Apr 14, 2014 at 11:41 AM 0
Share

Thanks every one, I found a solution. Posted as an answer!

1 Reply

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

Answer by smirlianos · Apr 14, 2014 at 11:41 AM

 var Block:GameObject;
 var squareSize : float = 1.0f;
  
 function Update () {
     if (Input.GetMouseButtonDown(0))
     {
         var mousePos : Vector3 = Input.mousePosition;
         mousePos.z = 10;
         var objectPos : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
         objectPos.x = Mathf.Round(objectPos.x/squareSize) * squareSize;
         objectPos.y = Mathf.Round(objectPos.y/squareSize) * squareSize;
         objectPos.z = Mathf.Round(objectPos.z/squareSize) * squareSize;
         Instantiate(Block, objectPos, Quaternion.identity);
     }
 }
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

23 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

Related Questions

I need to instantiate a bunch of objects without performance dips 1 Answer

Issue with photon instantiate 0 Answers

2D Animation does not start 1 Answer

2D coordinate grid over a 3D scene? 3 Answers

Random instantiate at same frame with each instantiate having unique random direction 1 Answer


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