• 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 jobo22 · Aug 01, 2017 at 07:35 PM · gridbuildingrtssnapsnapping

RTS building snap to grid

Hi I am trying to figure out how to get my building to snap to a grid when I move it around. Right now it follows my mouse position just fine, but I can't get it to snap. Please help!

     private Vector3 temp;
 
     // Update is called once per frame
     void Update () {
 
         
         temp.x = Mathf.Round(Input.mousePosition.x);
         temp.y = Mathf.Round(Input.mousePosition.y);
         temp.z = Camera.main.transform.position.y - 0.5f;
 
 
         transform.position = Camera.main.ScreenToWorldPoint(temp);

I just need the x and y to snap, the z position is just fine. Thanks! :)

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

2 Replies

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

Answer by jobo22 · Aug 02, 2017 at 06:49 PM

Finally got the snapping to work using this code

         Vector3 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.y));
         Vector3 finalPos = new Vector3(Mathf.Round(mousePos.x), Mathf.Round(mousePos.y), Mathf.Round(mousePos.z));
         transform.position = finalPos;

This is all in void Update()

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
avatar image
0

Answer by OrdinaryDev83 · Aug 01, 2017 at 07:41 PM

Try this :

 Vector3 finalPos = Vector3.zero;
 void Update(){
      Vector3 cursorPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
      finalPos = new Vector3(Mathf.Round(cursorPos.x), Mathf.Round(cursorPos.y), [Z value here]);
 }

Comment
Add comment · Show 3 · 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 jobo22 · Aug 01, 2017 at 08:13 PM 0
Share

It doesn't work. The cube wouldn't follow the mouse when I added transform.postion = finalPos, so I changed it to this

     private Vector3 finalPos = Vector3.zero;
     
     // Update is called once per frame
     void Update () {
         Vector3 cursorPos = Input.mousePosition;
         finalPos = new Vector3($$anonymous$$athf.Round(cursorPos.x), $$anonymous$$athf.Round(cursorPos.y), Camera.main.transform.position.y - 0.5f);
         transform.position = Camera.main.ScreenToWorldPoint(finalPos);

But it still wont snap to the grid. :(

avatar image OrdinaryDev83 jobo22 · Aug 01, 2017 at 08:31 PM 0
Share

It should work, it depends on your Camera settings.

avatar image jobo22 OrdinaryDev83 · Aug 01, 2017 at 08:48 PM 0
Share

I just have a default camera with no settings changed except its rotation and position. I will hopefully figure it out. Thank you for helping! :)

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

69 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 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 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

Snap to grid not working for me 1 Answer

Best solution for Grid-based building system? 0 Answers

An UI element will follow the mouse and snap to a grid, but why is the grid shifted after every game start? 0 Answers

How to drag gameobject (only x y) snapped to a grid? 2 Answers

Gizmo won't stop snapping to grid. 1 Answer

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