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 /
  • Help Room /
avatar image
0
Question by $$anonymous$$ · Feb 16, 2019 at 02:33 PM · gridsnappinggrids

Can I attach a grid to a moving GameObject?

Disclaimer: I am extremely new to C# and Unity. The solution I'm looking for is almost certainly beyond my skill level, but I would like to see if I'm on the right track or if my objective is at least feasible.


I want to attach a one-column grid (1x6) to a vertical GameObject (e.g. a sword) that is moved by the player. The idea is to create a 2D connect-three game where objects that fall from above snap onto the grid from the bottom up in a stacking fashion.

I have created a GameObject onto which I have added a C# script for a movable grid of adjustable dimensions. I have also added a CapsuleCollider to that GameObject (the grid and the collider have the same dimensions).

alt text


My intent is to have only objects that hit the collider snap to the grid (i.e. the player has to catch them with the sword).

My problems are:

1) I am not sure if it is possible to pair the collider with the grid such that only objects that collide with it snap to the grid.

2) I am not sure how to facilitate snapping to the grid during runtime.

I know that's a lot to tackle. Short of a coding solution, could someone let me know if the concept is reasonably feasible? Maybe offer suggestions as to where I might begin.

Thanks!


If it's helpful, the code for the grid, which I got from Youtube user "doppelgunner", is:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Sword_Grid_Script : MonoBehaviour
 
 {
 
     //grid specifics
     [SerializeField]
     private int rows;
     [SerializeField]
     private int cols;
     [SerializeField]
     private Vector2 gridSize;
     [SerializeField]
     private Vector2 gridOffset;
 
     //about cells
     [SerializeField]
     private Sprite cellSprite;
     private Vector2 cellSize;
     private Vector2 cellScale;
 
     void Start()
     {
         InitCells(); //Initialize all cells
     }
 
     void InitCells()
     {
         GameObject cellObject = new GameObject();
 
         //creates an empty object and adds a sprite renderer component -> set the sprite to cellSprite
         cellObject.AddComponent<SpriteRenderer>().sprite = cellSprite;
 
         //catch the size of the sprite
         cellSize = cellSprite.bounds.size;
 
         //get the new cell size -> adjust the size of the cells to fit the size of the grid
         Vector2 newCellSize = new Vector2(gridSize.x / (float)cols, gridSize.y / (float)rows);
 
         //Get the scales so you can scale the cells and change their size to fit the grid
         cellScale.x = newCellSize.x / cellSize.x;
         cellScale.y = newCellSize.y / cellSize.y;
 
         cellSize = newCellSize; //the size will be replaced by the new computed size, we just used cellSize for computing the scale
 
         cellObject.transform.localScale = new Vector2(cellScale.x, cellScale.y);
 
         //fix the cells to the grid by getting the half of the grid and cells add and minus experiment
         gridOffset.x = -(gridSize.x / 2) + cellSize.x / 2;
         gridOffset.y = -(gridSize.y / 2) + cellSize.y / 2;
 
         //fill the grid with cells by using Instantiate
         for (int row = 0; row < rows; row++)
         {
             for (int col = 0; col < cols; col++)
             {
                 //add the cell size so that no two cells will have the same x and y position
                 Vector2 pos = new Vector2(col * cellSize.x + gridOffset.x + transform.position.x, row * cellSize.y + gridOffset.y + transform.position.y);
 
                 //instantiate the game object, at position pos, with rotation set to identity
                 GameObject cO = Instantiate(cellObject, pos, Quaternion.identity) as GameObject;
 
                 //set the parent of the cell to GRID so you can move the cells together with the grid;
                 cO.transform.parent = transform;
             }
         }
 
         //destroy the object used to instantiate the cells
         Destroy(cellObject);
     }
 
     //so you can see the width and height of the grid on editor
     void OnDrawGizmos()
     {
         Gizmos.DrawWireCube(transform.position, gridSize);
     }
 }





screen-shot-2019-02-16-at-63459-am.png (167.1 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

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

Progrids not working with poly shape 0 Answers

How to make rotating objects with snapping to grid while the object contains children 0 Answers

Snap object to the local grid. 1 Answer

How do I account for rotation in a snap to function? 0 Answers

ProBuilder Element Snap to Grid (Unity 2020.1.2f1) 0 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