- Home /
Puzzle logic stuck
Hi All, I'm making a puzzle game in unity 2d with a grid of empty game objects with attached trigger box collider 2d and six cards (sprites) with trigger box colliders 2d. I have a scene with the six cells of the grid at 0.0f of Z position and six cards with sorting order "2" and this script attached:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Card : MonoBehaviour
{
public int num;
private Vector3 offset;
private void OnMouseDown()
{
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f));
this.GetComponent<SpriteRenderer>().sortingOrder = 3;
}
private void OnMouseUp()
{
this.GetComponent<SpriteRenderer>().sortingOrder = 2;
}
void OnMouseDrag()
{
Vector3 newPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f);
transform.position = Camera.main.ScreenToWorldPoint(newPosition) + offset;
}
private void OnTriggerEnter2D(Collider2D col)
{
Debug.Log(col.gameObject.name);
}
}
This prints all, even the underlying cards(but I need that the cards trigger the cells) , and the cards, after a first drag, stops to move on mouse drag... I've noticed taht if I omit OnMouseUp and OnTriggerEnter2D, it makes the drag always. Why? How can I fix the problem?
I want to try and help, but am having a hard time understanding what you are asking. Can you find a screenshot (or short gif or video) that shows an example of what you need, maybe give an example of a game that you are trying to mimick?
The logic is: six cards (sprites with boxcollider2d and rigidbody2d) that are instatiated randomly in a grid and six gameobjects with boxcollider2d displaced in a 2x3 grid named 1, 2, 3, 4, 5, 6 I need to drag around the instantiated cards (each of that have an int number from 1 to 6) onto the grid, and when the card 1 is on the grid 1, that card takes the position of the cell of the grid and is blocked there. Sorry no screenshot because I get "Error Parsing File"...
Answer by I_Am_Err00r · Jul 11, 2019 at 08:35 PM
So on each grid, it needs to check if the card placed on it is the right one?
If yes, change your ontriggerenter with what I have here (note I'm not using an IDE, so if there are spelling mistakes or something like that I can't see them, ALSO BE SURE TO ADD THIS TO VARIABLES: int theMatchingID)
OnTriggerEnter2D(Collider2D col)
{
if(col.gameObject == grid.gameObject //or something like that
{
theMatchingID = col.GetComponent<WhateverClassStoresTheGridID>()gridID // or something like that
CheckCardInt(num);
}
}
And then add this function:
CheckCardInt(int ID)
{
if(ID == theMatchingID // or something like that
{
ThenWeHaveAMatchingID(); //or something like that
}
}
Try that, I think it should work.
Excuse me, it is't clear: I haven't a Gameobject "grid" in the class "Card", so the lines
col.gameObject == grid.gameObject
and
col.GetComponent<WhateverClassStoresTheGridID>().gridID
are not possible.. Shuld I add a script to every cell with the var
public int gridID;
And do I need to add the var
public GameObject grid;
in Cards.cs?
I've understood why On$$anonymous$$ouseDown and On$$anonymous$$ouseUp don't work: if I disable the cells they works, with the sorting order and thet drag... I think that the problem is the Z position of the cells but I'bve tried both positive and negative positions and it doesn't work anyway... I have the cells inside an empty GameObject all positioned to Z 0.0f and the cards positioned at Z 0.0f...
$$anonymous$$aybe the offset ScreenToWorldPoint Z at 10.0f is wrong?
Hey man, I'm sorry I gave you a solution that had a ton of fill in the blank work on your end.
What I can do is write a couple scripts later tonight and post them on here which will make the solution I gave hopefully work.
Use this for the card script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Card : $$anonymous$$onoBehaviour
{
public int num;
int matchingID;
private Vector3 offset;
private void Start()
{
}
private void On$$anonymous$$ouseDown()
{
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f));
this.GetComponent<SpriteRenderer>().sortingOrder = 3;
}
private void On$$anonymous$$ouseUp()
{
this.GetComponent<SpriteRenderer>().sortingOrder = 2;
}
void On$$anonymous$$ouseDrag()
{
Vector3 newPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f);
transform.position = Camera.main.ScreenToWorldPoint(newPosition) + offset;
}
private void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Grid") //or something like that
{
matchingID = col.GetComponent<GridID>().ID; // or something like that
CheckCardInt(num);
}
}
public void CheckCardInt(int ID)
{
if (ID == matchingID) // or something like that
{
//this is where you would add the code you need to because the card ID match
}
}
}
}
In the inpsector assign num for each card.
Put this script on whatever object you store the grids in:
using UnityEngine;
public class GridID : $$anonymous$$onoBehaviour
{
public int ID;
}
Then assign the value of ID in the inspector for each grid item.
One last very important thing, you need to add a tag and make sure each grid object has that tag, name the tag Grid (exactly as it appears there, it needs to be referenced in the script by exact matching characters). I you don't know how to add a tag, click here: https://www.youtube.com/watch?v=EA54-vfLkUI Now I don't know if I fully understand your end goal, but it sounds like you just want to drag a card and drop it on the appropriate grid, this will do that assu$$anonymous$$g the card you are dragging has the same ID, you just have to remember to set the 1-6 values yourself in the inspector.
Answer by areFranz · Jul 15, 2019 at 01:46 PM
Ok, I came up with this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Card : MonoBehaviour
{
public int num;
public int matchingID;
public bool moved;
private GameObject logic;
private Vector3 offset;
private void Start()
{
logic = GameObject.Find("PuzzleLogic");
moved = false;
}
void OnMouseDown()
{
this.GetComponent<SpriteRenderer>().sortingOrder = 3;
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f));
}
void OnMouseUp()
{
this.GetComponent<SpriteRenderer>().sortingOrder = 2;
}
void OnMouseDrag()
{
moved = true;
Vector3 newPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f);
transform.position = Camera.main.ScreenToWorldPoint(newPosition) + offset;
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Grid" && moved == true)
{
matchingID = col.GetComponent<Cells>().gridID;
CheckCard(num, col.gameObject);
}
}
private void OnTriggerStay2D(Collider2D col)
{
if (col.gameObject.tag == "Grid" && moved == true)
{
matchingID = col.GetComponent<Cells>().gridID;
CheckCard(num, col.gameObject);
}
}
void CheckCard(int ID, GameObject col)
{
if (ID == matchingID)
{
Debug.Log(ID);
//logic.GetComponent<PuzzleLogic6>().count++;
transform.position = col.gameObject.transform.position;
this.GetComponent<SpriteRenderer>().sortingOrder = 2;
this.GetComponent<Card>().enabled = false;
}
}
}
This seem to work: the cards lock in the position of the cells but I have a problem: It prints the ID even if the dragged card is not over the matching ID... Why?
Hi, I've been helping you, I started a new Unity account specfic to these types of tutorials outside of my existing Unity account.
The first thing I would look at is if any of the cards underneath grid actually do have the matching ID, because with 2D games there is no z axis and if all your cards are stacked on top of each other like that, when you move the card over a grid, it will collide with everything underneath the card that is actually on top, so check that.
If none of the cards underneath grid do have the matching ID, then Debug.Log to all of the other references to the matchingID, find out if they match what you set as that grid in the inspector during runtime; the code you have there doesn't show that the matchingID or the num variables being changed by the script you provided, so you might not have it setup properly in the inspector or another script is sneaking in and changing the values.
One quick thing, why are you doing both ontriggerenter and ontriggerstay? It shouldn't mess up the code you have, but is not necessary, and you probably just want ontriggerenter.
HI, excuse me for the late: I don't work in week ends... I made a video to show you the whole behaviour of the puzzle game... link text
The cells are in Z position 1.0f and the cards are at the Z position 0.0f, id I change that values the cards don't snap the grid. The OnTriggerStay() is needed because I need that the cards snaps teh grid even if I drag them around and don't only when they enters the trigger after they exit it... But the matchingID that is printed is always the id of the card and not the id of teh cell (as you can see in the video). I hope that this video opens your $$anonymous$$d, because it doesn't open the one fo $$anonymous$$e...