简体   繁体   中英

Unity 2D: Check if sprites overlap (c#)

I'm making a small matching mini-game in 2D Unity in which players drag and drop different images(sprites) that are in the same category. I have made a drag and drop script that works well, but I am not sure how to go about checking if the images are dropped in the correct place. If the images are matched incorrectly, I want the image that the player had dragged over to transform back to its original spot. If the images are correctly matched, I want both to disappear. How would I check if the images overlap.

Drag and drop script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DragandDrop : MonoBehaviour {

private bool selected;

private void Update()
{
    //Mouse is held down
   if(selected == true)
    {
        Vector2 cursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        transform.position = new Vector3(cursorPos.x, cursorPos.y, -1.0f);
    } 

   //Mouse is released
   if(Input.GetMouseButtonUp(0))
    {
        ScoreScript.scoreValue += 1/6f;
        selected = false;
        //Ignore these:
        // Vector3 position = transform.position;
        // position[2] = 0;
        // transform.position = position;


        //This is where I think I should check for overlap
    }

}

void OnMouseOver() {
    if(Input.GetMouseButtonDown(0)) {
        selected = true;
    }
}
}

That should work:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ImageScript : MonoBehaviour {

private bool selected;
Vector3 currentPosition;
ImageScript secondImage;
void Update()
{
    if (selected == true)
    {
    Vector2 cursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    transform.position = new Vector3(cursorPos.x, cursorPos.y, -1.0f);

    } 

//Mouse is released
    if(Input.GetMouseButtonUp(0))
    {
        if (Vector3.Distance(transform.position, secondImage.transform.position) < 1f)
        {
            //Distance to second image is less than 1 on mouse up which makes the both images disappear.

            ScoreScript.scoreValue += 1/6f;
            selected = false;
            Destroy(SecondImage);
            Destroy(this);
        }
        else
        {
            //Distance to second image on mouse up is higher than 1 which resets the position back to original.
            transform.position = currentPosition;
        }
        //Ignore these:
        // Vector3 position = transform.position;
        // position[2] = 0;
        // transform.position = position;


        //This is where I think I should check for overlap
}

}
void OnMouseOver() {
if(Input.GetMouseButtonDown(0)) 
{
    selected = true;
    currentPosition = transform.position;
    }
}

Make sure to assign ImageScript of second image to current one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM