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 psychedelik · Nov 05, 2011 at 04:59 PM · javascriptgridplane

placing block on plane in a grid

i made a minecraft like game where i need to place block block on a plane i aready figurered out how to place block side to side but now what i whant to do is being able to ceperate a plane in a grid and place my block on the grid insted of in the center of the plane.how can i do this?

here is the block placing part of my script :

 var hit : RaycastHit;
 
 var range : float = 15.0f;
 
 var woodBlock:Transform;
 
 
 
 function Update () {
 
     if (Input.GetMouseButtonDown(0) && Hit()) {
 
         var cube : Transform = Instantiate(woodBlock);
 
         cube.transform.position = hit.transform.position + hit.normal;
 
     }
 
     if (Input.GetMouseButtonDown(1) && Hit() &&hit.transform.gameObject.tag == "BuildMat"){
 
          Destroy(hit.transform.gameObject);
 
         }
 
         
 
 
 
 }
 
 
 
 function Hit() : boolean {
 
     return Physics.Raycast(transform.position, transform.forward, hit, range);
 
 }
Comment
Add comment · Show 1
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 chemicalvamp · Nov 06, 2011 at 12:18 PM 0
Share

If you want your chunks to center correctly on the chunk you clicked on to create the new one, than using plane as a floor may not be the best way to do it, i mean you do something like if(hit.transform.gameObject.tag == "Plane"){ round to nearest acceptable position } where hit.point is the world position of the place you clicked on the plane.. but it may be a bit easier to create a floor out of chunks.

1 Reply

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

Answer by Jehos · Nov 06, 2011 at 04:42 PM

I didn't understand much your question, but if each cell of your "grid" has the same size (as it definitely should be), then just calculate based on the click position.

Here's a very small example:

  • Let's say cell size is 10 by 10 units

  • Grid size is 20 cells wide by 20 cells long

  • Also, let's say the plane is located to that the (0,0,0) world coordinate is on the lower-left of the plane (just like when you create Terrains). This means the plane itself is not located at (0,0,0), so it's not "centered". This is not mandatory but if you change this, you have to add offsets to all calculations.

If the mouse click is at (138, 0, 179) -world position-, then you calculate like so:

cellSize = 10; mouseX = 138; mouseZ = 179; (we ignore mouse Y since we don't care)

columnClicked = Mathf.Floor(mouseX / cellSize); // in our example, the column clicked is 13 rowClicked = Mathf.Floor(mouseY / cellSize); // in our example, the row clicked is 17

So, at this point, you now the user clicked on the cell located in the coordinate (13, 17). To get the center of it, just add half the cellSize to the position coordinate. Meaning: cell 1 starts at X=0 and ends at X=10 horizontally; start at Z=0 and ends at Z=10 vertically cell 2 starts at X=10 and ends at X=20 and so forth... so, cell 13 starts at X=130 and ends at X=140

To get the center of the cell add half the size to it. So, the center of cell 13 would be at X=135.

Also, you should use Mathf.Floor or Mathf.Ceil depending if you want the cell coordinate to be zero-based.

I hope this helps and makes sense.

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 psychedelik · Nov 07, 2011 at 12:57 AM 0
Share

thanx for your time :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Resizable grid of gameobjects 1 Answer

Instantiate prefab on a grid pattern? 1 Answer

Changing materials of a plane on keydown. 1 Answer

How to add accelerometer controls to this script? 0 Answers

Grid Based Movement (Pokemon-like) 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