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 sam.ibbitson · Jun 05, 2011 at 05:18 PM · instantiategridprops

How to Instantiate a prefab onto a grid?

Dear UnityAnswers community,

I've looted a bit of code from someone else to help facilitate a little dungeon creator I'm working on. The code is placed in the main player camera and it spawns a little cube when the player clicks on the mouse.

Reference:

 var building : GameObject;
 var gridX = 5;
 var gridY = 5;
 var spacing = 2.0;
 function Update () 
 {
     if(Input.GetMouseButtonDown(0))
     {
         Build();
     }
 }
 function Build()
 {
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hit : RaycastHit;
         if (Physics.Raycast (ray, hit, 100)) 
         {
             Debug.DrawLine (ray.origin, hit.point);
             Instantiate(building,hit.point,Quaternion.identity);
         }
 }

What I need to work out is how best to get the cube to follow some kind of grid and only create a new prefab along certain X-Y-Z axis!

May I point out that this isn't going to be a MineCraft clone- I'm simply using the cube as a starting point and will actually replace this with in game prefabs such as walls, doors, etc... However, I suppose the principal is the same although the objects I have in mind may be several blocks high/long/wide for certain 'props'.

Thanks in advance

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
3
Best Answer

Answer by Owen-Reynolds · Jun 06, 2011 at 01:44 AM

Suppose the grid is 5x5 squares. That means you want to snap to center points of 2.5, 7.5, 12.5 ... . First compute the grid square you are on, then scale for size, then center it.

You currently instantiate at the hit.point:

 Instantiate(explosionPrefab, hit.point, rot);

Instead, recompute the grid center near hit.point and spawn there:

 float3 P = hit.point;
 P.x = Mathf.Floor(P.x/5) * 5 + 2.5f;
 // same for z
 // If your ground isn't flat, look up P.y = groundHT at x,z
 // If you might be on a rock, do a new raycast down from P to get new HT/normal

 // spawn using corrected point:
 Instantiate(explosionPrefab, P, Quaternion.identity);

If you camera is always aimed straight down, you could probably do the math early, after screenPointToRay, to avoid the 2nd raycast.

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 sam.ibbitson · Jun 08, 2011 at 06:52 PM 0
Share

Amazing, thanks for your help Owen!

avatar image Anxo · Jun 08, 2011 at 07:10 PM 0
Share

mark as answered please.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Problems creating a grid in Runtime 1 Answer

Trying to create grid, "Use of unassigned local variable" error for prefab of tile although it is assigned? 0 Answers

How to instantiate a prefab at mouse pos 1 Answer

Grid-based building system not working 1 Answer

Add Item to GridLayoutGroup using Instantiate 3 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