I have created a 2d drag and drop game using mouse control. But I don’t know how to add a score when the correct object drags to the correct box. what I need is, when the object overlaps the correct object the score must be increased. This is my code,
public class Drag : MonoBehaviour{
public GameObject correctForm,carrot,circle;
private bool moving;
private bool finish;
private float startPosX;
private float startPosY;
private Vector3 resetPosition;
void Start()
{
resetPosition = this.transform.localPosition;
}
void Update()
{
if(finish == false)
{
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 OnMouseDown()
{
if(Input.GetMouseButtonDown(0))
{
Vector3 mousepos;
mousepos = Input.mousePosition;
mousepos = Camera.main.ScreenToWorldPoint(mousepos);
startPosX = mousepos.x - this.transform.localPosition.x;
startPosY = mousepos.y - this.transform.localPosition.y;
moving = true;
}
}
public void OnMouseUp()
{
moving = false;
if(Mathf.Abs(this.transform.localPosition.x - correctForm.transform.localPosition.x) <= 0.5f &&
Mathf.Abs(this.transform.localPosition.y - correctForm.transform.localPosition.y) <=0.5f)
{
this.transform.position = new Vector3(correctForm.transform.position.x, correctForm.transform.position.y, correctForm.transform.position.z);
//MyscoreText.text = "Score : " + ScoreNum;
//Destroy(coin);
finish = true;
if(moving == false)
{
Score.ScoreNum += 2;
}
}
else
{
this.transform.localPosition = new Vector3(resetPosition.x, resetPosition.y, resetPosition.z);
}
}
}