- Home /
Question by
darkhog · Jan 10, 2013 at 12:53 AM ·
c#mousemouse-dragdraganddrop
Problem with dragging objects
I have following script for dragging things:
using UnityEngine;
using System.Collections;
public class DragTile : MonoBehaviour {
//declaring some variables
private bool mousedwn;
void OnMouseDown() {
if (!GameData.freemove){
mousedwn = true;
}
}
void OnMouseUp() {
mousedwn = false;
}
// Update is called once per frame
void Update () {
if ((mousedwn) && (Input.GetMouseButtonDown(0))){
this.transform.Translate(Input.GetAxis("Mouse X"),0,Input.GetAxis("Mouse Y"),Space.World);
}
}
}
However it doesn't work as planned. It works fine if I change
if ((mousedwn) && (Input.GetMouseButtonDown(0)))
into
if (mousedwn)
but then I can't tell the difference between buttons. How can I fix it?
Comment
Best Answer
Answer by Lovrenc · Jan 10, 2013 at 01:04 AM
Use
Input.GetMouseButton(0)
instead.
GetMouseButtonDown returns true ONLY for the frame when mouse button WAS CLICKED. So when mouse button changed its state to being pressed.