How to position world space canvas from script
Hi guys! Im bashing my head against a wall for some time, figured i should ask you!
The situation is the following:
I want to place a world space canvas next to the top-right corner of the collider of an object. The whole object is created runtime, and the collider to fit the icon of the object so i have to reposition my canvas from script.
This is my canvas (note the scaling!):
This is the hierarchy:
Basically the scaling (which is neccessary because its world space) screws up everything. When i tried with scaling 1,1,1, I successfuly positioned it with this:
newPos = new Vector2(renderer.sprite.bounds.max.x, renderer.sprite.bounds.max.y)
trans.localPosition = new Vector3(newPos.x + (trans.pivot.x * trans.rect.width), newPos.y - ((1f - trans.pivot.y) * trans.rect.height), trans.localPosition.z);
But if the scaling is back to the original it scews up badly.
Any suggestions, approaches?
Your answer
![](https://koobas.hobune.stream/wayback/20220612094447im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
UI Elements repositioning and resizing when playing game 1 Answer
AddListener fuction throwing a NullReferenceException error 1 Answer
SetActive(false) in Start does not seem to work 2 Answers
Health bars above enemies but for some reason while I move, they are offsetted. 1 Answer
Get the position of a picture on the screen. Related to canvas scaler 0 Answers