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 snowy6409 · May 18, 2015 at 10:16 AM · gridgrids

I need to Snap a 3d object to a grid when it is placed in game.

Hello Unity answers community. I have looked all over the pages and could not find a script for snapping a 3d object to a grid when placed in the run time. I have a script hat is using a ray-cast to get the position of the courser that is locked at the center of the screen.

 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);
          }
  }

I need the Object (Cube) to be snapped to a grid when the object is spawned in.

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

Answer by Cherno · May 18, 2015 at 10:47 AM

This is more a basic math question. You need to take the XYZ values of the transform.position and round it to the nearest multiple of you grid size.

There are a lot of examples on the net, here is one that should suit your needs, might need modification due to floating point numbers, but the basic theory is the same:

How to round an integer up or down to the nearest 10 using Javascript

find nearest multiple of x

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 snowy6409 · May 18, 2015 at 12:12 PM 0
Share

Thanks Cherno But you are right the floating point numbers are my down fall. If there is anyway to not do floating point or geting it appliable for floating point numbers.

avatar image Cherno · May 18, 2015 at 12:22 PM 0
Share

Please post comments as comments and answers as answers :) (I converted you answer to a comment).

You can try casting ints as floats with (float)yourInt and use $$anonymous$$ath.RoundtoInt.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Grid snapping 0 Answers

dragging objects horizontally in a grid or in increments 2 Answers

Grids for begginers? 0 Answers

Make grid and snap to it in Unity editor (2D Game) 2 Answers

Snapping to grid with grid size of <= 1 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