Scaling to be a specific height in Unity - c#

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.
I need a generic formula so the paddle height is always 1/3 of the screen height.
Thanks in advanced for any help!

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);
}

Related

joystick positioning relative to screen size

I want the joystick fixed on the bottom-right the screen, no matter the resolution/window size. Although my solution works, there must be a cleaner code for this or some canvas option I don't know about.
I tried and tested a position that works on a set resolution and then factor the new resolution to that. Tested on resolutions: 434x494 and 1920x1080
For 434 pixel wide screen a joystick position.x of 434 - 115 = 319.
The factor was too big so I had to square root it for width.
For the height 120 worked for 494 height screen, I just did some hocus pocus to make it work for 1920x1080.
public Joystick joystick;
public Vector3 m_myPosition;
public RectTransform m_NewTransform;
void Start()
{
double width = Screen.width - (Mathf.Sqrt(Screen.width / 434)) * 115;
double height = 120 + (4* Mathf.Exp(Screen.width / 494));
m_myPosition.x = (float)width;
m_myPosition.y = (float)height;
m_NewTransform.position = m_myPosition;
}
I shouldn't rely on iffy code like this, any solutions?
G'day folks
Solution is everywhere on youtube...
You just change the canvas UI scale to 'Scale to screen size' and then Anchor the UI joystick object to a direction.
For my joystick I just anchored it to bottom-right and done!
In case you want in depth info on how to; I followed this tutorial: https://www.youtube.com/watch?v=w3sMD-3OJro

Unity - drag to resize / scale plane based on handle position?

Alright, I cant find any example of this already done and my attempt is yielding odd results - I need to drag to resize a flattened cube (like a plane, but must have thickness so it was a cube) using a handle in the corner. Like a window on your desktop.
So far Ive created my handle plane and gotten it via script attached to my cube plane. The cube plane and the handle are children of an empty to which the scale is being applied so that the cube plane scales from left to right as desired:
That works, however my attempt at using the delta scale handle position to scale the parent empty scales either way too much or in odd directions:
Awake() {
scaleHandle = GameObject.FindGameObjectWithTag("ScaleHandle");
scaleHandleInitialPos = scaleHandle.transform.localPosition;
}
Update()
{
width += -1*(scaleHandleInitialPos.x - scaleHandle.transform.localPosition.x);
height += -1 * (scaleHandleInitialPos.y - scaleHandle.transform.localPosition.y);
transform.parent.localScale = new Vector3(width, height, thickness);
}
what is wrong here?
Hierarchy:
Transform of the child:
With updating initialPos every Update() OR doing width = scaleHandle.transform.localPosition.x / scaleHandleInitialPos.x; where the white square is the scaleHandle

Accelerometer movement limit according to screen size

I am using this code:
void Update()
{
accel = Input.acceleration.x;
transform.Translate (0, 0, accel);
transform.position = new Vector3 (Mathf.Clamp (transform.position.x,-6.9f, 6.9f), -4.96f, 18.3f);
}
It makes my gameObject move the way I want it BUT the problem is that when I put the app on my phone, (-6.9) and (6.9) are not the "ends" of my screen. And I cannot figure out how to change those values according to every screen size?
This is going to be a bit of a longer answer, so please bear with me here.
Note that this post is assuming that you are using an orthographic camera - the formula used won't work for perspective cameras.
As far as I can understand your desire is to keep your object inside of the screen boundaries. Screen boundaries in Unity are determined by a combination of camera size and screen size.
float height = Camera.main.orthographicSize * 2;
A camera's orthographic size determines the half of the screen's size in world units. Multiplying this value results in the amount of world units between the top and bottom of the screen.
To get the width from this value, we take the value and multiplying it by the screen's width divided by the screen's height.
float width = height * Screen.width / Screen.height;
Now we have the dimensions of our screen, but we still need to keep the object inside those bounds.
First, we create an instance of the type Bounds, which we will use to determine the maximum and minimum values for the position.
Bounds bounds = new Bounds (Vector3.zero, new Vector3(width, height, 0));
Note that we used Vector3.zero since the center of our bounds instance should be the world's center. This is the center of the area that your object should be able to move inside.
Lastly, we clamp the object's position values, according to our resulting bounds' properties.
Vector3 clampedPosition = transform.position;
clampedPosition.x = Mathf.Clamp(clampedPosition.x, bounds.min.x, bounds.max.x);
transform.position = clampedPosition;
This should ensure that your object will never leave the screen's side boundaries!

Scaling issues with Unity

I am trying to get the starting paddle (I am creating pong) to be at the left most of the screen at all times. I am having trouble coming up with a good formula for doing this. My camera size is 5, it is centered at (0,0). The paddle should be at approximately -6.5 for the x-coordinate. Here, in the script for the paddle, is how I am trying to do this
//renderer is the SpriteRenderer
xPos = -Camera.main.orthographicSize -
(renderer.sprite.textureRect.width/ renderer.sprite.pixelsPerUnit);
However this results in this (the left paddle is here the code is being applied):
So, the right paddle is essential being placed at the very left - the width of the image. However, some of the paddle image is cut off. I can't seem to figure out why and I have checked everywhere. Any help would be extremely appreciated and noticed.
I think your problem is related to the calculation of the width of the screen.
Unity uses Camera.main.orthographicSize to calculate the screen's height and then calculates the width depending on the screen proportions. Try to calcule the width in this way:
width= Camera.main.orthographicSize * Camera.main.aspect;

How do I make DrawString() take the current viewport into account when drawing, like Draw() does?

Currently, all my textures are being scaled and move into the right position when the viewport that I draw them in changes size or position (I don't have to do any calculations myself to do this).
However, when I use DrawString() (while still in the same viewport), and the viewport changes size or position, the text follows no logic that I can figure out. It scales as expected, but it moves in a very weird way.
Here's
(to get the main player's name to move correctly when scaling down, I came up with this bad "formula": X -= Viewport.X / 2.15f. Y -= Viewport.X / 3.2f)
Now, is there any way to make DrawString() work like Draw() does when it comes to scaling with viewports?
The way I've got it set up now is:
_spriteBatch.GraphicsDevice.Viewport = ScreenGame.Viewport;
// Draw tile sprites
// Draw player sprites
// Draw text
My apologies in advance if I've forgotten to mention something relevant.
On the DrawString() try
x /= Scale.X;
y /= Scale.Y;
This is of course pseudo code. I think if you can find the new scale and store it in a struct or something, you can do this to scale the text's location properly.

Categories

Resources