- Home /
Drag and Drop lagging behind mouse
I am trying to implement a 2D sprite drag and drop script. However, when I move a 2D object (with a 2D box collider) the transform does not keep up with the mouse movement, but actually lags behind. The mouse moves as expected, but the object only follows the mouse movement very very slowly... (aka if you move the mouse across the whole screen, the box may move 0.5cm for example). Obviously one cannot use this script as it is, any clue what I am doing wrong? Am I missing anything in my code?
This code should work with any 2D sprite that has a 2D box collider on it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragAndDropSprites : MonoBehaviour {
private float startPosX;
private float startPosY;
private bool isBeingHeld = false; // is it being held by mouse?
public Vector3 initialPosition;
// Use this for initialization
void Start () {
initialPosition = this.gameObject.transform.position;
Debug.Log(initialPosition.ToString());
}
// Update is called once per frame
void Update () {
if(isBeingHeld == true)
{
Vector3 mousePos; //initialise empty vector
mousePos = Input.mousePosition; // Get position as vector
mousePos = Camera.main.ScreenToViewportPoint(mousePos); // get position in game world... rather than position
this.gameObject.transform.localPosition = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, 0);
}
}
private void OnMouseDown()
{
if (Input.GetMouseButtonDown(0)) // 0 is left mouse button [1 is right mouse button]
{
Vector3 mousePos; //initialise empty vector
mousePos = Input.mousePosition; // Get position as vector
mousePos = Camera.main.ScreenToViewportPoint(mousePos); // get position in game world... rather than position
//mousePos = Camera.main.ScreenToWorldPoint(mousePos); // get position in game world... rather than position
// offset
startPosX = mousePos.x - this.transform.localPosition.x;
startPosY = mousePos.y - this.transform.localPosition.y;
isBeingHeld = true;
}
}
private void OnMouseUp()
{
isBeingHeld = false;
}
}
Your answer
Follow this Question
Related Questions
OnBeginDrag, OnEndDrag not being called for unknown reasons 1 Answer
How to drag Unity Objects to other c# application 0 Answers
Possible to drag items across tabs onto the same frame? 1 Answer
How to drag a cube in orthographic camera? 0 Answers
How do i disable my buttons while dragging ScrollRect? 0 Answers