void TouchOrMouseChange(InputHandler handler) { if (!this.isTouched) { return; } InputHandlerPointer pointer = (InputHandlerPointer)handler; Vector3 worldPosition = Camera.main.ScreenToWorldPoint(pointer.currentPosition); Vector3 newPosition = new Vector3(worldPosition.x + offset.x, worldPosition.y + offset.y, gameObject.transform.position.z); Vector3 deltaPosition = this.previousPosition - newPosition; // This will lock the movement along one of the two axis if (this.onlyMoveAlongAxis) { this.direction = Math.Abs(deltaPosition.x) > Math.Abs(deltaPosition.y) ? AxisDirection.horizontal : AxisDirection.vertical; float xPos = (this.direction == AxisDirection.horizontal) ? (worldPosition.x + offset.x) : transform.position.x; float yPos = (this.direction == AxisDirection.vertical) ? (worldPosition.y + offset.y) : transform.position.y; newPosition = new Vector3(xPos, yPos, gameObject.transform.position.z); } this.previousPosition = newPosition; this.DragDidContinue(newPosition); }
void TouchOrMouseStart(InputHandler handler) { InputHandlerPointer pointer = (InputHandlerPointer)handler; Vector3 wp = Camera.main.ScreenToWorldPoint(pointer.currentPosition); Vector2 touchPos = new Vector2(wp.x, wp.y); // Make sure you are touching this object foreach (Collider2D targetCollider in colliders) { if (targetCollider == Physics2D.OverlapPoint(touchPos)) { Debug.Log("Collider pressed: " + targetCollider.gameObject.name); if (targetCollider == resetCollider) { ResetCode(); } else if (targetCollider == enterCollider) { CheckIfEvent(); } else { NumberPressed(targetCollider.gameObject.name); } } } }
void TouchOrMouseStart(InputHandler handler) { // TouchOrMouse event is of (sub-)type InputHandlerPointer // Debug.Log("Touch or mouse start"); InputHandlerPointer pointer = (InputHandlerPointer)handler; Vector3 wp = Camera.main.ScreenToWorldPoint(pointer.currentPosition); Vector2 touchPos = new Vector2(wp.x, wp.y); // Make sure you are touching this object if (GetComponent <Collider2D>() == Physics2D.OverlapPoint(touchPos)) { gameObject.transform.rotation = Quaternion.identity; // Reset the rotation if (spriteWhenDragging != null) { gameObject.GetComponent <SpriteRenderer>().sprite = spriteWhenDragging; } // If you are already on a plate, move back to your original if (hitTargetOn != null) { ReturnToOriginalPosition(); } else { isTouched = true; } } }
void TouchOrMouseStart(InputHandler handler) { InputHandlerPointer pointer = (InputHandlerPointer)handler; Vector3 wp = Camera.main.ScreenToWorldPoint(pointer.currentPosition); Vector2 touchPos = new Vector2(wp.x, wp.y); // Make sure you are touching this object foreach (Collider2D targetCollider in colliders) { if (targetCollider == Physics2D.OverlapPoint(touchPos)) { Debug.Log("Collider pressed: " + targetCollider.gameObject.name); if (selectedCollider == null) { selectedCollider = targetCollider; Helper.PlayAudioIfSoundOn(selectedSound); selectedCollider.gameObject.GetComponent <SpriteRenderer>().enabled = true; } else if (selectedCollider == targetCollider) { Investigate(); } else { Helper.PlayAudioIfSoundOn(selectedSound); selectedCollider.gameObject.GetComponent <SpriteRenderer>().enabled = false; selectedCollider = targetCollider; selectedCollider.gameObject.GetComponent <SpriteRenderer>().enabled = true; } } } }
void TouchOrMouseChange(InputHandler handler) { if (isTouched) { InputHandlerPointer pointer = (InputHandlerPointer)handler; Vector3 worldPosition = Camera.main.ScreenToWorldPoint(pointer.currentPosition); Vector3 newPos = worldPosition + offset; LayerMask mask = 1 << 13; Collider2D hit2 = Physics2D.OverlapPoint(new Vector2(newPos.x, newPos.y), mask); if (hit2 != null) { if (hit2.gameObject.name == "TrunkArea") { gameObject.transform.position = new Vector3(newPos.x, newPos.y, gameObject.transform.position.z); } else { isTouched = false; } } else { isTouched = false; } } }
void TouchOrMouseChange(InputHandler handler) { // Debug.Log("Touch or mouse change"); if (isTouched) { // Debug.Log("Actually is this object"); InputHandlerPointer pointer = (InputHandlerPointer)handler; Vector3 worldPosition = Camera.main.ScreenToWorldPoint(pointer.currentPosition); gameObject.transform.position = new Vector3(worldPosition.x, worldPosition.y, gameObject.transform.position.z); } }
void TouchOrMouseChange(InputHandler handler) { if (isTouched) { InputHandlerPointer pointer = (InputHandlerPointer)handler; Vector3 worldPosition = Camera.main.ScreenToWorldPoint(pointer.currentPosition); gameObject.transform.position = new Vector3(worldPosition.x + offset.x, worldPosition.y + offset.y, gameObject.transform.position.z); } }
// This is the event handler function void TouchOrMouseHandler(InputHandler handler) { // TouchOrMouse event is of (sub-)type InputHandlerPointer InputHandlerPointer pointer = (InputHandlerPointer)handler; // Update this with the current pointHistoryLength, // in case you'd experiment with it in the editor pointer.pointHistoryLength = pointHistoryLength; // Start the coroutine which draws the line for us StartCoroutine(LineDrawer(pointer)); }
// Update is called once per frame void TouchOrMouseStart(InputHandler handler) { InputHandlerPointer pointer = (InputHandlerPointer)handler; Vector3 wp = Camera.main.ScreenToWorldPoint(pointer.currentPosition); Vector2 touchPos = new Vector2(wp.x, wp.y); if (GetComponent <Collider2D>() == Physics2D.OverlapPoint(touchPos)) { offset = gameObject.transform.position - wp; isTouched = true; } }
void TouchOrMouseChange(InputHandler handler) { if (this.isTouched) { this.isDragged = true; InputHandlerPointer pointer = (InputHandlerPointer)handler; Vector3 worldPosition = Camera.main.ScreenToWorldPoint(pointer.currentPosition); Vector3 newPos = worldPosition + this.offset; gameObject.transform.position = new Vector3(newPos.x, newPos.y, gameObject.transform.position.z); } }
public void TouchOrMouseChange(InputHandler handler) { if (isTouched) { GetComponent <AudioSource>().enabled = true; InputHandlerPointer pointer = (InputHandlerPointer)handler; Vector3 dir = Camera.main.ScreenToWorldPoint(pointer.currentPosition); float newTouchPos = Mathf.Atan2(-dir.x, dir.y) * Mathf.Rad2Deg; newAngle = newTouchPos + originalAngle - cachedAngle; // Account for (new touch position - original touch position) and the original rotation angle of the ring // Debug.Log("name: " + name + " NTP: " + newTouchPos + " origAngle: " + originalAngle + " cached: " + cachedAngle + " newA: " + newAngle); transform.rotation = Quaternion.AngleAxis(newAngle, Vector3.forward); } }
void TouchOrMouseStart(InputHandler handler) { InputHandlerPointer pointer = (InputHandlerPointer)handler; Vector3 wp = Camera.main.ScreenToWorldPoint(pointer.currentPosition); Vector2 touchPos = new Vector2(wp.x, wp.y); foreach (SelectCorrectItemPiece piece in pieces) { if (piece.GetComponent <Collider2D>() == Physics2D.OverlapPoint(touchPos)) { piece.isSelected = !piece.isSelected; CheckIfWin(); } } }
// Private Methods void TouchOrMouseStart(InputHandler handler) { InputHandlerPointer pointer = (InputHandlerPointer)handler; Vector3 wp = Camera.main.ScreenToWorldPoint(pointer.currentPosition); Vector2 touchPos = new Vector2(wp.x, wp.y); // Make sure you are touching this object if (GetComponent <Collider2D>() == Physics2D.OverlapPoint(touchPos)) { this.GetComponent <SpriteRenderer>().sortingOrder = 10; this.isTouched = true; this.isDragged = false; this.offset = gameObject.transform.position - wp; } }
void TouchOrMouseStart(InputHandler handler) { InputHandlerPointer pointer = (InputHandlerPointer)handler; originalPosition = Camera.main.ScreenToWorldPoint(pointer.currentPosition); // Get the position of the initial touch so you know how much to rotate relative to this point cachedAngle = Mathf.Atan2(-originalPosition.x, originalPosition.y) * Mathf.Rad2Deg; // Make sure you are touching this object Vector2 touchPos = new Vector2(originalPosition.x, originalPosition.y); if (GetComponent <Collider2D>().OverlapPoint(touchPos)) { GetComponent <AudioSource>().enabled = false; isTouched = true; } }
// Private methods void TouchOrMouseStart(InputHandler handler) { // Reset states this.isTouched = false; this.direction = AxisDirection.none; InputHandlerPointer pointer = (InputHandlerPointer)handler; Vector3 worldPoint = Camera.main.ScreenToWorldPoint(pointer.currentPosition); Vector2 touchPosition = new Vector2(worldPoint.x, worldPoint.y); this.offset = gameObject.transform.position - worldPoint; if (GetComponent <Collider2D>() == Physics2D.OverlapPoint(touchPosition)) { this.isTouched = true; this.previousPosition = gameObject.transform.position; this.DragDidStart(); } }
void TouchOrMouseStart(InputHandler handler) { if (isAnimating) { return; } InputHandlerPointer pointer = (InputHandlerPointer)handler; Vector3 wp = Camera.main.ScreenToWorldPoint(pointer.currentPosition); Vector2 touchPos = new Vector2(wp.x, wp.y); foreach (Collider2D pieceCollider in allPieces) { if (pieceCollider == Physics2D.OverlapPoint(touchPos)) { // You can select the piece if its renderer is the opposite of isEnablePieceWhenTapped. if (isSteppingStoneRoom || pieceCollider.GetComponent <Renderer>().enabled == !isEnablePieceWhenTapped) { lastPieceTapped = pieceCollider; // Cache because HandlePieceTapped can trigger later // If you need to have an item to do tap in correct order, check it now. if (!HasCorrectItem()) { return; } if (selectedSound) { Helper.PlayAudioIfSoundOn(selectedSound); } if (!MoveToTapLocation(lastPieceTapped.transform.position)) { HandlePieceTapped(); // Otherwise handle piece tapped at the end of MoveToTapLocation } } } } }
void TouchOrMouseStart(InputHandler handler) { InputHandlerPointer pointer = (InputHandlerPointer)handler; Vector3 wp = Camera.main.ScreenToWorldPoint(pointer.currentPosition); Vector2 touchPos = new Vector2(wp.x, wp.y); // Make sure you are touching this object if (GetComponent <Collider2D>() == Physics2D.OverlapPoint(touchPos)) { gameObject.transform.rotation = Quaternion.identity; // Reset the rotation offset = gameObject.transform.position - wp; isTouched = true; if (moveToDefaultSortingLayerIfDragging) { SpriteRenderer[] spriteRenderers = GetComponentsInChildren <SpriteRenderer>(); foreach (SpriteRenderer childRenderer in spriteRenderers) { childRenderer.sortingLayerName = "Default"; } } } }
void TouchOrMouseStart(InputHandler handler) { if (isAnimating) { return; } InputHandlerPointer pointer = (InputHandlerPointer)handler; Vector3 wp = Camera.main.ScreenToWorldPoint(pointer.currentPosition); Vector2 touchPos = new Vector2(wp.x, wp.y); foreach (Collider2D pieceCollider in allPieces) { if (pieceCollider == Physics2D.OverlapPoint(touchPos)) { lastPieceTapped = pieceCollider; if (selectedSound) { Helper.PlayAudioIfSoundOn(selectedSound); } HandlePieceTapped(); } } }
// Draw lines for as long as they last IEnumerator LineDrawer(InputHandlerPointer pointer) { GameObject clone = Instantiate(goLineRenderer) as GameObject; clone.SetActive(true); LineRenderer lineRenderer = clone.GetComponent <LineRenderer>(); int linesize = 0; // While the pointer is still active while (pointer.status != EventStatus.End) { // Update the LineRenderer linesize = pointer.positions.Count; lineRenderer.positionCount = linesize; int j = 0; for (int i = pointer.positions.Count - 1; i >= 0; i--) { lineRenderer.SetPosition( j++, // vertex #i Camera.main.ScreenToWorldPoint(pointer.positions[i].position + Vector3.forward) // touch or mouse is in screen space, convert to world space ); } yield return(null); } while (linesize > 0) { lineRenderer.positionCount = --linesize; yield return(null); } Destroy(clone); }
void TouchOrMouseChange(InputHandler handler) { if (isTouched) { InputHandlerPointer pointer = (InputHandlerPointer)handler; Vector3 worldPosition = Camera.main.ScreenToWorldPoint(pointer.currentPosition); Vector3 newPos = worldPosition + offset; float newX = newPos.x; float newY = newPos.y; if (colliderBounds != null) { Bounds bounds = colliderBounds.bounds; maxX = bounds.max.x; minX = bounds.min.x; maxY = bounds.max.y; minY = bounds.min.y; newX = Mathf.Clamp(newX, minX, maxX); newY = Mathf.Clamp(newY, minY, maxY); // Debug.Log("MaxX: " + maxX + " MinX: " + minX + " maxY: " + maxY + " minY: " + minY + " newX: " + newX + " newY: " + newY); } if (clampX) { newX = gameObject.transform.position.x; } if (clampY) { newY = gameObject.transform.position.y; } gameObject.transform.position = new Vector3(newX, newY, gameObject.transform.position.z); } }