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 amithreddy93 · Oct 25, 2020 at 06:58 PM · grid based gamesnapping

Placing Objects on a grid,

Hi, I am new to coding and am working on a new game project, I created a grid and have taken all the grid positions in a list, now I need to snap my objects to the grid positions. I did it but there comes a problem I don't want another game object to snap to the position where a previous game object is already snapped, can you help here is my code

public class Newdragdrop : MonoBehaviour { private bool isbeingheld = false; public List GridPositionFinal = Grid.GridPosition; private int index; public List GridFilledPositions; Vector3 closestObject;

 private List<Vector3> objectpositions;

 private void Update()
 {
     
     if(isbeingheld == true)
     {
         Vector3 mousepos;
         mousepos = Input.mousePosition;
         mousepos = Camera.main.ScreenToWorldPoint(mousepos);
         this.gameObject.transform.localPosition = new Vector3(mousepos.x, mousepos.y, -0.1f);
     }
     
 }
 private void OnMouseDown()
 {
     if (Input.GetMouseButtonDown(0))
     {
         Vector3 mousepos;
         mousepos = Input.mousePosition;
         mousepos = Camera.main.ScreenToWorldPoint(mousepos);
         isbeingheld = true;
     }
     

 }
 private void OnMouseUp()
 {
     Vector3 mousepos1;
     mousepos1 = Input.mousePosition;
     mousepos1 = Camera.main.ScreenToWorldPoint(mousepos1);
     var distance = 1.5f;
     
     foreach (var snapgrid in GridPositionFinal)
     {
         var currentSlotDistance = Mathf.Sqrt(Mathf.Pow((mousepos1.x-snapgrid.x),2.0f)+ Mathf.Pow((mousepos1.y - snapgrid.y), 2.0f));
         if (currentSlotDistance < distance)
         {
             
             closestObject = new Vector3(snapgrid.x, snapgrid.y, 0);
             

         }

     }
     if (mousepos1.x > -4.0f && mousepos1.y < 3.2f)
     {
         this.gameObject.transform.localPosition = new Vector3(closestObject.x,closestObject.y,0);
         

         
     }

     
 }
  

}

,

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

Answer by Klarzahs · Oct 26, 2020 at 08:59 AM

Hi @amithreddy93,

Is your grid, well, grid like, meaning does it have a rigid and evenly spaced offset between each possible position? If so, you can find your actual grid position like so (untested):

  mousepos1 = Input.mousePosition;
  mousepos1 = Camera.main.ScreenToWorldPoint(mousepos1);
  gridPos = new Vector2(mousePos1.x - mousePos1.x%OFFSETX, mousePos1.y - mousePos1.y % OFFSETY);

You basically take the overshoot over the possible positions and subtract it. That way you don't actually need to save all the possible positions beforehand, and you especially don't have to loop all over them each time

To actually save/check for duplicates, it depends on whether you expect to have a filled grid, or a rather empty one. If you want to have a filled grid in the end, you can use a 2D array as a lookup. If you want to have a rather empty grid, you can still use an array, but it will be mostly empty too. Which is wasted memory :) So you can use a Dictionary<Vector2, GameObject> as a lookup

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

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

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

NGUI drag and drop snap 1 Answer

Grid snapping 0 Answers

Prevent this script from snapping diagonally 0 Answers

How to create a cube in 2d view and scale it based on a point/anchor? Than visualize the cube in 3d. 1 Answer

Grid-based building system not working 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