void OnMouseDrag() { if (!Input.GetMouseButton(0)) { return; } //Press delete or backspace, then delete if (Input.GetKeyDown(KeyCode.Delete) || Input.GetKeyDown(KeyCode.Backspace)) { Destroy(gameObject); } var mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); //avoids raycasting to ourself disableColldiers(); var hit = Physics2D.Raycast(mouseWorldPos, Vector2.zero); if (hit.collider != null) { //did we hit a IsoObject var hitIsoObject = hit.collider.GetComponent <IsoTransform>(); if (hitIsoObject != null) { //calc the new height were we want to put our object at var newHeight = hitIsoObject.Position.y + hitIsoObject.Size.y / 2f + isoObject.Size.y / 2f; //calc the final position var pos = Isometric.CreateXYZfromY(Input.mousePosition, newHeight); if (pos != null) { //pos = Ceil(pos); if (FindObjectsOfType <IsoTransform>().All(c => c.Position != pos)) { isoObject.Position = pos.Value; } } } } else { //we didn't hit anything, keep the current height var pos = Isometric.CreateXYZfromY(Input.mousePosition, isoObject.Position.y + isoObject.Size.y / 2f); //pos = Ceil(pos); if (pos != null) { isoObject.Position = pos.Value; } } //turn colliders back on enableColldiers(); }
void Update() { if (!selected && Input.GetKeyDown(KeyCode.Alpha1)) { select(); } if (selected && Input.GetMouseButtonDown(0) && Input.mousePosition.y >= 150) { Vector3 pos = (Vector3)Isometric.CreateXYZfromY(Input.mousePosition, 0); if (GridManager.get().cut((int)(Math.Round(pos.x)) - 1, (int)(Math.Round(pos.z)) - 1)) { if (!Input.GetKey(KeyCode.LeftShift) && !Input.GetKey(KeyCode.RightShift)) { leaveSelection(); } } } }
void Update() { if (Input.GetMouseButtonDown(0) && cursorPlant != null && Input.mousePosition.y >= 145) { Vector3 pos = (Vector3)Isometric.CreateXYZfromY(Input.mousePosition, 0); if (Main.get().money >= selectedPlant.cost) { if (GridManager.get().placePlant((int)(Math.Round(pos.x)) - 1, (int)(Math.Round(pos.z)) - 1, selectedPlant)) { Main.get().money -= selectedPlant.cost; if (!Input.GetKey(KeyCode.LeftShift) && !Input.GetKey(KeyCode.RightShift)) { leaveSelection(); } Main.msgInsertArbre(); } } } if (Input.GetMouseButtonDown(1)) { Vector3 pos = Isometric.CreateXYZfromY(Input.mousePosition, 0).Value; int x = (int)Math.Round(pos.x) - 1; int y = (int)Math.Round(pos.z) - 1; if (x >= 0 && x < GridManager.get().getSize() && y >= 0 && y < GridManager.get().getSize()) { Main.openDescription(x, y); } } if (selectedPlant != null) { IsoTransform iso = cursorPlant.GetComponent <IsoTransform> (); float xdec = iso.Position.x - (float)Math.Round(iso.Position.x); float zdec = iso.Position.z - (float)Math.Round(iso.Position.z); try { Vector3 pos = Isometric.CreateXYZfromY(Input.mousePosition, 0).Value; pos.x = (float)Math.Round(pos.x) + xdec; pos.z = (float)Math.Round(pos.z) + zdec; pos.y = iso.Position.y; if (pos.x > 0 && pos.z > 0 && pos.x <= GridManager.get().size&& pos.z <= GridManager.get().size) { iso.Position = pos; } else { iso.Position = new Vector3(-1.0f + xdec, iso.Position.y, -1.0f + zdec); } } catch (System.InvalidOperationException) { iso.Position = new Vector3(-1.0f + xdec, iso.Position.y, -1.0f + zdec); } } }