Image bounds miscalculated?
So I'm trying to detect if the player is dragging his mouse over a transparent sprite and I noticed weird behavior, so I used a DrawLine to see the bounds of the box and I'm getting weird results that don't make sense. I made certain that every piece is the correct one and I checked that the piecePos and pieceBounds make sense. Yet when I run the code, this happens The red lines should be going from corner to corner of their respective boxes, and yet it isn't even halfway there, it's a little more than halfway there. I account for the scale of the parent object named slidingLockPieces. Any ideas what's happening? I tried to not account for the scale, but that made it too big.
GameObject piece = pieces[numPiecesSwiped]; //get the next piece and its data Vector3 piecePos = piece.transform.position; Vector3 pieceBounds = piece.GetComponent().sizeDelta / 2 * slidingLockPieces.transform.localScale.x; Debug.DrawLine(piecePos - pieceBounds, piecePos + pieceBounds, Color.red, 3.0f);
Answer by OakQ · Sep 12, 2017 at 07:57 PM
Figured it out. The reason for the miscalculation is because the canvas was set to Scale with screen size. The solution is to account for the scaling
CanvasScaler canvasScaler = canvas.GetComponent(); float screenAdjust = Screen.height / canvas.referenceResolution.y; piece.transform.position; Vector3 pieceBounds = piece.GetComponent().sizeDelta / 2 * slidingLockPieces.transform.localScale.x * screenAdjust;
Your answer
Follow this Question
Related Questions
Programatically creating and setting UI image 0 Answers
Given volume dimensions and direction, how to determine if Vector3 is contained 2 Answers
Image Colour change not working with anythign other than base preset colours 2 Answers
RectTransform Left Right Bottom Top 0 Answers
Help! Animating hogs my Pos Y. 0 Answers