Hi all!
I’m working on some simple projects while I get a feel for Unity’s editor. Right now, I have this basic script that controls camera movement around a simple 2D map of arbitrary size:
void Update()
{
if (Input.GetMouseButton(0))
{
Cursor.visible = false;
float hor = Input.GetAxis("Mouse X");
float vert = Input.GetAxis("Mouse Y");
float hadjust = hor * speedmult * cam.orthographicSize * 0.1f;
float vadjust = vert * speedmult * cam.orthographicSize * 0.1f;
Vector3 move = new Vector3(hadjust * -1, vadjust * -1, 0.0f);
cam.transform.Translate(move);
}
if(!Input.GetMouseButton(0) && !Cursor.visible)
{
Cursor.visible = true;
}
}
It works well enough for me, and even has a few bells and whistles (adjusting the movement based on the camera’s FOV + hiding the cursor). But in looking at some stuff online, I found that a lot of people use OnMouseDrag() rather than checking Input.GetMouseButton(0) on Update(). OnMouseDrag() doesn’t work for this purpose, because right now, my 2D map consists of only a Grid and Tilemap rather than any GUIElements or Colliders.
I don’t want to learn bad habits, so my question is this: is there a reason to not use this code and instead have some kind of screen-filling invisible collider to detect OnMouseDrag()? Or is it better to keep this code as-is so that I can implement clickable/dragable elements later? I’m okay with some slight messiness, but I’d like to approach things in a way that results in as few errors later on as possible.
Any feedback is welcome, and I’d definitely like to hear how any of you would approach this script. Thank you!