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).
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);
}
}
Your answer
Follow this Question
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