Pookie
Pookie

Reputation: 1279

Scaling to be a specific height in Unity

I have a game like Pong, except you control both paddles and try to keep the ball in bounds. I want the paddle to always be 1/3 of the height of the screen for all resolutions. So if the Height = 900, then the paddle should be 300 high. For some reason, I can't seem to find a way that works for all resolutions anywhere. I feel I need to accomplish this through a script. Here is an image of the components for my paddle.

enter image description here

I need a generic formula so the paddle height is always 1/3 of the screen height.

Upvotes: 1

Views: 395

Answers (1)

Cress
Cress

Reputation: 432

Get the canvas height and assign the paddle sizeDelta to have y size equal to the canvas height divided by 3.

This is the script, assumed you attach it to the paddle:

public Canvas myCanvas;

void Start() {
    float canvasHeight = myCanvas.pixelRect.height;
    GetComponent<RectTransform>().sizeDelta = new Vector2(GetComponent<RectTransform>().sizeDelta.x, canvasHeight/3f);
}

Upvotes: 1

Related Questions