• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Generate a grid at a specific position 1 Answer

Limiting size of a tilemap grid and populating it through a file 0 Answers

Grid and or Tile System 0 Answers

Grid snapping 0 Answers

dragging objects horizontally in a grid or in increments 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges