private void Update() { Ray ray = camera.ScreenPointToRay(Input.mousePosition); // Clone to the function (checks if the ray is colliding with a connection or a pin) RaycastHit hit; if (Physics.Raycast(ray, out hit, 100)) // 100 is the max distance the ray reaches (consumes less resources) { if (hit.transform.gameObject.tag == "Connection" && !isDragging && (Input.GetMouseButtonDown(2) || Input.GetKey("delete") || Input.GetKey("backspace"))) // Destroys connection if right clicked { Destroy(hit.transform.gameObject); } if (hit.transform.gameObject.tag == "Pin") // The prefab has a tag called "Pin" { isPin = true; } else { isPin = false; } } else { return; } if (Input.GetMouseButtonDown(0) && !isDragging && isPin) // If there is no dragging line, start one { isDragging = true; PinWrapper tempWrap = GetPointInPlane(); if (tempWrap == null) { return; } firstPosition = tempWrap.point; firstPin = tempWrap.pin ?? new Pin(); lines.Add(Instantiate(connectorPrefab, firstPosition, Quaternion.identity)); lines[index].transform.parent = connectionsParent.transform; } if (Input.GetMouseButton(0) && isDragging && index < lines.Count && index >= 0) // If player is dragging change the last position of the connector { PinWrapper tempWrap = GetPointInPlane(); if (tempWrap == null) { Destroy(lines[index]); isDragging = false; isPin = false; return; } lastPosition = tempWrap.point; lastPin = tempWrap.pin ?? new Pin(); lines[index].transform.rotation = Quaternion.Euler(0, 0, (Mathf.Atan2(firstPosition.y - lastPosition.y, firstPosition.x - lastPosition.x) * 180f / Mathf.PI) - 90); lines[index].transform.localScale = new Vector3(1, Vector3.Distance(firstPosition, lastPosition) / 2f, 1); } if (Input.GetMouseButtonUp(0)) // Resetting the position of the positions for next time { isDragging = false; // Dragging stops if (isPin) // If you stop dragging in a pin the connector stops so you can make a new one { lines[index].name = lines[index].GetInstanceID().ToString(); player.AddNode( (firstPin.hint ?? (new Hint())), (lastPin.hint ?? (new Hint())) //lines[index].GetInstanceID() ); firstPosition = Vector3.zero; lastPosition = Vector3.zero; index++; player.RandomWin(); } // FIX ME: Check if current index has a line or not, only destroy it when it does else if (index < lines.Count) //lines[index] != null) // Else, you will destroy the current line, so you can start a new one (if on corkboard) { Destroy(lines[index]); lines.RemoveAt(index); } } }
// Use this for initialization public void Init(int x, int y) { position = new Vector2(x, y); pw = transform.FindChild("PinWrapper").GetComponent("PinWrapper") as PinWrapper; }