我正在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;
}
}
}
答案 0 :(得分:0)
应该可以:
html, body{
overflow-y: auto;
height: 500px;
position: relative;
}
<div id="myDIV" style="position: absolute; bottom: 0px; width: 50%; border: 1px solid black;">
Div
</div>
<button style="float: right;" onClick="myFunction()">
Toggle Bottom Property of #myDiv
</button>
<script>
function myFunction() {
var x = document.getElementById("myDIV");
x.style.bottom === "0px" ? x.style.bottom = "300px" : x.style.bottom = "0px";
}
</script>
确保将第二张图像的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;
}
}
分配给当前图像。