- Home /
New game objects stuck in the air...Match 3
So, I'm having some issues with a Match 3 game that I'm working on. When the game objects are matched and a new set of game objects appear to fil the missing spots. The new game objects get stuck in the air. I have no idea where the issue is coming from.
I think the issue is coming from my game board script, I've included the three scripts that I have created that way whoever is able to help me with this might be able to more easily see the issue without having to completely recreate the game.
Board.cs
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Board : MonoBehaviour { //Descrition of boxes to hold the music notes public List gems = new List(); public int GridWidth; public int GridHeight;
//prefab of Assets
public GameObject gemPre;//box go
public Gem lastGem;
//new vector to switch asset places
public Vector3 gem1Start,gem1End, gem2Start,gem2End;
public bool isSwapping = false;
public Gem gem1, gem2;
public float startTime; //time to move
public float swapRate = 2; //how fast notes move
public int AmountToMatch = 3; //notes needed to create a match
public bool isMatched = false;
// Use this for initialization
void Start()
{
//creates the game board using the boxes
for (int y = 0; y < GridHeight; y++)
{
for (int x = 0; x < GridWidth; x++)
{
GameObject g = Instantiate(gemPre, new Vector3(x, y, 0),Quaternion.identity)as GameObject;
g.transform.parent = gameObject.transform;
gems.Add(g.GetComponent<Gem>());
}
}
//centers gameboard
gameObject.transform.position = new Vector3(-2f, -2.3f, 0);
}
// Update is called once per frame
void Update ()
{
if(isMatched)
{
for(int i=0;i<gems.Count;i++)
{
if(gems[i].isMatched)
{
gems[i].CreatePiece();
gems[i].transform.position = new Vector3(
gems[i].transform.position.x,
gems[i].transform.position.y +6,
gems[i].transform.position.z);
}
}
isMatched = false;
}
if(isSwapping)
{
MoveGem(gem1, gem1End, gem1Start);
MoveNegGem(gem2, gem2End, gem2Start);
if (Vector3.Distance(gem1.transform.position, gem1End) < .1f || Vector3.Distance(gem2.transform.position, gem2End) < .1f)
{
gem1.transform.position = gem1End;
gem2.transform.position = gem2End;
gem1.ToggleSelector();
gem2.ToggleSelector();
lastGem = null;
isSwapping = false;
TogglePhysics(false);
checkMatch();
}
}
}
public void checkMatch()
{
List<Gem> gem1List = new List<Gem>();
List<Gem> gem2List = new List<Gem>();
ConstructMatchList(gem1.color, gem1, gem1.XCoord, gem1.YCoord, ref gem1List);
FixMatchList(gem1, gem1List);
ConstructMatchList(gem2.color, gem2, gem2.XCoord, gem2.YCoord, ref gem2List);
FixMatchList(gem2, gem2List);
print("Gem1 " + gem1List.Count);
}
public void ConstructMatchList(string color, Gem gem, int XCoord, int YCoord, ref List<Gem> MatchList)
{
if (gem == null)
{
return;
}
else if (gem.color != color)
{
return;
}
else if (MatchList.Contains(gem))
{
return;
}
else
{
MatchList.Add(gem);
if (XCoord == gem.XCoord || YCoord == gem.YCoord)
{
foreach (Gem g in gem.NextDoor)
{
ConstructMatchList(color, g, XCoord, YCoord, ref MatchList);
}
}
}
}
public void FixMatchList (Gem gem, List ListToFix) { List rows = new List(); List collumns = new List(); // bool rowsMatched = false; // bool collumnsMatched = false; for(int i=0; i = AmountToMatch) { isMatched = true; for(int i=0;i = AmountToMatch) { isMatched = true; for (int i = 0;i
public void MoveGem(Gem gemToMove, Vector3 toPos,Vector3 fromPos)
{
Vector3 center = (fromPos + toPos) *.5f;
center -= new Vector3(0, 0, .01f);
Vector3 riseRelCenter = fromPos - center;
Vector3 setRelCenter = toPos - center;
float fracComplete = (Time.time - startTime)/swapRate;
gemToMove.transform.position = Vector3.Slerp(riseRelCenter, setRelCenter, fracComplete);
gemToMove.transform.position += center;
}
public void TogglePhysics(bool isOn) { for(int i =0; i
startTime = Time.time;
gem1 = lastGem;
gem2 = currentGem;
isSwapping = true;
}
else
{
lastGem.ToggleSelector();
lastGem = currentGem;
}
}
}
}
Gems.cs
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Gem : MonoBehaviour { //array of colors to apply to the pieces string[] gemMats = { "Black", "Blue", "Green", "Orange", "Purple", "Red", "Yellow" }; public string color = ""; //Game piece public GameObject sphere; public GameObject selector; //List of neighboring gamepieces public List NextDoor = new List();
public bool isSelected = false; //is the note selected
public bool isMatched = false; //is note matched
public int XCoord
{
get
{
return Mathf.RoundToInt(transform.localPosition.x);
}
}
public int YCoord
{
get
{
return Mathf.RoundToInt(transform.localPosition.y);
}
}
// Use this for initialization
void Start ()
{
CreatePiece();
}
// Update is called once per frame
void Update ()
{
}
public void ToggleSelector()
{
isSelected = !isSelected;
selector.SetActive(isSelected);
}
//Add and Remove NextDoor determines if a box is next to another box or not
public void AddNextDoor(Gem g)
{
if (!NextDoor.Contains(g))
{
NextDoor.Add(g);
}
}
public bool IsNextDoorWith(Gem g)
{
if(NextDoor.Contains(g))
{
return true;
}
return false;
}
public void RemoveNextDoor(Gem g)
{
NextDoor.Remove(g);
}
//creates game piece
public void CreatePiece() { //applies color to pieces color = gemMats[Random.Range(0, gemMats.Length)]; Material m = Resources.Load("Materials/" + color) as Material; sphere.GetComponent().material = m; isMatched = false;
}
//determine if box is clicked
void OnMouseDown()
{
ToggleSelector();
if (!GameObject.Find("Board").GetComponent<Board>().isSwapping)
{
GameObject.Find("Board").GetComponent<Board>().SwapGems(this);
}
}
}
Bump.cs
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Bump : MonoBehaviour {
public Gem owner;
void OnTriggerEnter(Collider c)
{
if (c.tag == "Gem")
{
owner.AddNextDoor(c.GetComponent<Gem>());
}
}
void OnTriggerExit(Collider c)
{
if (c.tag == "Gem")
{
owner.RemoveNextDoor(c.GetComponent<Gem>());
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Money System Scripting Problem 0 Answers
Create more text in unity GUI? (C#) 2 Answers
StartCoroutine not working 3 Answers