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().sortingOrder = 3;
}
private void OnMouseUp()
{
this.GetComponent().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?
Trending Articles
More Pages to Explore .....