- Home /
Question by
cagatayakarca · Aug 26, 2019 at 09:48 PM ·
drag-and-drop
How to auto-set sprites in a position while dragging?
While dragging a sprite I want them to set a position automatically within stated coordinates. Problem is within close range sprites sets in a position while still holding mouse button down. What I was trying to was them to set only if I stopped to pushing mouse button. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class dragdrop : MonoBehaviour
{
private float startposx;
private float startposy;
private bool isBeeingheld = false;
void Update()
{
if (isBeeingheld == true)
{
Vector3 mousePos;
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
Debug.Log(mousePos.y);
{
if (mousePos.y > 2)
{
this.gameObject.transform.localPosition = new Vector3(1, 3, 0);
}
else { this.gameObject.transform.localPosition = new Vector3(mousePos.x, mousePos.y, 0); }
}
}
}
private void OnMouseDown() {
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePos;
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
isBeeingheld = true;
}
}
private void OnMouseUp() {
isBeeingheld = false;
}
}
Comment