【发布时间】:2020-07-24 16:19:57
【问题描述】:
我正在 2D Unity 中制作一个小型匹配迷你游戏,玩家可以在其中拖放同一类别的不同图像(精灵)。我制作了一个运行良好的拖放脚本,但我不确定如何检查图像是否放置在正确的位置。如果图像匹配不正确,我希望玩家拖过的图像变回原来的位置。如果图像正确匹配,我希望两者都消失。如何检查图像是否重叠。
拖放脚本:
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;
}
}
}
【问题讨论】: