我可以在移动的GameObject上附加网格吗?

时间:2019-02-19 23:24:19

标签: unity3d

编辑:我一直在将这篇文章中的标签从Unity3d更改为Unity2d,并且在我发布时它会不断自动更改回3d。抱歉给您带来不便。

我想将单列网格(1x6)附加到玩家移动的垂直GameObject(例如剑)上。这个想法是要创建一个2D Connect-three游戏,其中从上方掉落的对象从下往上以堆叠的方式捕捉到网格上。

我创建了一个GameObject,并在其中添加了C#脚本,用于可调整尺寸的可移动网格。我还向该GameObject添加了CapsuleCollider(网格和对撞机具有相同的尺寸)。

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

}

0 个答案:

没有答案