Могу ли я прикрепить сетку к движущемуся GameObject? - PullRequest
0 голосов
/ 20 февраля 2019

Редактировать: я продолжаю менять тег на этом посте с Unity3d на Unity2d, и он автоматически возвращается к 3d при публикации.Приносим извинения за неудобства.

Я хочу прикрепить сетку из одного столбца (1x6) к вертикальному объекту GameObject (например, мечу), который перемещается игроком.Идея состоит в том, чтобы создать 2D-игру connect-three, в которой объекты, падающие сверху, складываются в сетку снизу вверх.

Я создал GameObject, в который я добавил сценарий C # дляподвижная сетка регулируемых размеров.Я также добавил CapsuleCollider к этому GameObject (сетка и коллайдер имеют одинаковые размеры).

image

Мое намерение состоит в том, чтобы иметь только объекты, попадающие вколлайдер привязан к сетке (т.е. игрок должен поймать их мечом).

Мои проблемы:

1) Я не уверен, возможно ли связать коллайдер ссетка такая, что только объекты, которые сталкиваются с ней, привязываются к сетке.

2) Я не уверен, как упростить привязку к сетке во время выполнения.

Я знаю, что это много для решения.Если не считать решения по кодированию, может кто-нибудь сообщить мне, является ли концепция разумной?Может быть, предложить предложения о том, где я мог бы начать.

Спасибо!

Если это полезно, код для сетки, который я получил от пользователя Youtube "doppelgunner", является:

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);
 }

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...