I’m currently working on a mario maker style game and it’s got a bug that I don’t know how to fix, can anyone help?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class DragDrop : MonoBehaviour
{
public GameObject[] correctForm;
public float startPosX, startPosY;
public bool moving;
public GameObject preFab;
public GameObject Redsquare;
private Vector3 resetPosition;
private int ArrayValue = 0;
/// <summary>
/// Start is called on the frame when a script is enabled just before
/// any of the Update methods is called the first time.
/// </summary>
void Start()
{
resetPosition = this.transform.localPosition;
}
/// <summary>
/// Update is called every frame, if the MonoBehaviour is enabled.
/// </summary>
void Update()
{
if(moving)
{
Vector3 mousePos;
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
this.gameObject.transform.localPosition = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, this.gameObject.transform.localPosition.z);
}
}
public void RedSquare()
{
Instantiate(Redsquare);
}
private void OnMouseDown()
{
if(Input.GetMouseButtonDown(0))
{
Vector3 mousePos;
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
startPosX = this.transform.localPosition.x;
startPosY = this.transform.localPosition.y;
moving = true;
}
}
private void OnMouseUp()
{
moving = false;
if(Mathf.Abs(this.transform.localPosition.x - correctForm[ArrayValue].transform.localPosition.x) <= 0.5f &&
Mathf.Abs(this.transform.localPosition.y - correctForm[ArrayValue].transform.localPosition.y) <= 0.5f)
{
this.transform.localPosition = new Vector3(correctForm[ArrayValue].transform.localPosition.x, correctForm[ArrayValue].transform.localPosition.y, correctForm[ArrayValue].transform.localPosition.z);
Instantiate(preFab);
ArrayValue ++;
}
else
{
this.transform.localPosition = new Vector3(resetPosition.x, resetPosition.y, resetPosition.z);
}
}
}