- Home /
Trying to get the top of a Canvas Object.
Hello, I'm currently trying to get the top of a canvas object using Unity in 2D mode. The transform positions give me the center of object but I would like to get the top bound.
The reason is that I am currently setting up a start menu screen which displays the games title on a background (which I will later have animated) along with some particle effects going on (lightning striking and rain, ect). I am currently trying to get the lightning to start at the top of the the canvas.
The Canvas' Render mode is set up as Screen Space - Camera because I wanted to ensure that it resizes for different aspects and I wanted to be able to set its order in a sorting layer such that I could draw the lightning over it and its child components.
Currently it is positioned at a Y of 181 under its transform.
The height listed is 557.
However the actual top border of the canvas rests at something around 221.64.
Rotation is 0, and scale is 0.16502 for X, Y, and Z axises.
I has originally assumed that the difference between the center point of the rectangle and the upper bound would be half its height so I did the following (I was planning to change the values under Random.Range to follow suit if I could get this to work):
startPos = new Vector2(Random.Range(300, 500), ((CanvasUsed.GetComponent<RectTransform>().rect.height / 2) +
CanvasUsed.GetComponent<RectTransform>().transform.position.y));
However as I'm sure you'll notice the difference between 221~ and 181 (40) is not half of 557 (278~). Now I'm looking for alternatives to figure out where to get the position of the top of the canvas. I was considering just adding a blank object as a child of the canvas and positioning it via anchor where I want, then using the position of that, however, it feels a little bit weird for a workaround and I'm sure this kind of thing will come up again in the future. Thank you for your time.
Your answer
Follow this Question
Related Questions
Problem moving a panel UI 0 Answers
Text Colour Gradually Change Animation 0 Answers
Change Text Scale on a Canvas Child from a script. 1 Answer
Set Native Size in UI 1 Answer
Good way to scale 3D GameObjects in a Canvas (Screen Space - Camera)? 1 Answer