- Home /
Canvas elements don't change position with screen size
I have to instantiate the darts only inside the bigger square (refer image). Here is the code I used to try to add the boundaries. outerSquare
and the darts
are both Canvas UI elements (images specifically). This code, however, breaks on different screen sizes(refer image).
public GameObject outerSquare;
private RectTransform rt;
private Vector2 positionToSpawn;
private void Start()
{
rt = (RectTransform)outerSquare.transform;
}
void GetAreaToSpawn()
{
float minX = outerSquare.transform.position.x - (rt.rect.width / 2);
float maxX = minX + rt.rect.width;
float minY = outerSquare.transform.position.y - (rt.rect.height / 2);
float maxY = minY + rt.rect.height;
float posX = Random.Range(minX, maxX);
float posY = Random.Range(minY, maxY);
positionToSpawn = (new Vector2(posX, posY));
}
How can I adjust the darts' position according to screen size? The positioning like the first image(where the game view is smaller) is desired. Canvas Scaler
is set to Scale with Screen Size
. I tried working around Reference Resolution, but that doesn't give the desired output.
Your answer
Follow this Question
Related Questions
How to draw UI images with command buffers? 0 Answers
Canvas Pivot in Screen Space Overlay 0 Answers
Difficulties with world space canvas interaction 1 Answer
Why can't I see my full canvas? 1 Answer
Columns/Magic Jewelry remake on Unity 0 Answers