any idea how to do this? All i know is i need to change some code on onmousedrag but i do not know how.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FirstPart : MonoBehaviour
{
[SerializeField]
private Transform redPlace;
private Vector2 initialPosition;
private Vector2 mousePosition;
private float deltaX, deltaY;
public static bool locked;
void Start()
{
initialPosition = transform.position;
}
private void OnMouseDown()
{
if (!locked)
{
deltaX = Camera.main.ScreenToWorldPoint(Input.mousePosition).x - transform.position.x;
deltaY = Camera.main.ScreenToWorldPoint(Input.mousePosition).y - transform.position.y;
}
}
private void OnMouseDrag()
{
if (!locked)
{
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector2(mousePosition.x - deltaX, mousePosition.y - deltaY);
}
}
private void OnMouseUp()
{
if (Mathf.Abs(transform.position.x - redPlace.position.x) <= 0.5f &&
Mathf.Abs(transform.position.y - redPlace.position.y) <= 0.5f)
{
transform.position = new Vector2(redPlace.position.x, redPlace.position.y);
locked = true;
}
else
{
transform.position = new Vector2(initialPosition.x, initialPosition.y);
}
}