- Home /
Pong: Placing Gameobjects in a way that will work with any resolution
Hello,
I've just finished making a pong clone in 1024*768. When I build the game, in most resolutions, a wider screen area is being displayed than in the game's native resolution. In particular, varying amounts of space behind the paddles is being displayed.
I'd like some way to make the game consistent across resolutions. I've started by trying to scale the walls (thin horizontal cubes at the top and the bottom of the screen, for the balls to bounce off), but it doesn't seem to be working. This is the script I'm attaching to the walls:
using UnityEngine;
using System.Collections;
public class ScaleScript : MonoBehaviour
{
public GameObject attached;
// Use this for initialization
void Start ()
{
// Get the gameobject this script is attached to
attached = this.gameObject;
// Get the scale of the attached gameobject
Vector3 scale = attached.transform.localScale;
// Multiply the scale by the screenWidth divided by the native Screen Width
scale.x = scale.x * (IntroGui.screenWidth/1024);
// Resize the attached gameobject's transform
transform.localScale = scale;
}
}
you tell nothing about whether this is 2.5D or whatever
in any event, essentially, you have to work in "fractions of the screen width/height". OR you have to really clearly work in the UNITY WORLD SPACE, and ensure the camera is right each time.
it's a real nuisance really !
It's just regular top-down 2d. I'm not sure how to position objects relative to screen width - so far I have only been using the transform's position.
O$$anonymous$$, go to the doc and search on" "world to screen"
unity makes it easy !
I'm not 100% sure on this answer, but I've been experimenting with stuff like this, and I know my way isn't perfect, but it's getting there. You need to play around with positions such as 'Screen.height' and 'Screen.width'. If you want something in the middle, it would be something along the lines of
GUI.Label(Rect(Screen.height/2, Screen.width/2, 100, 100), "Hi");
It's not perfect, all you really need to do is play around with the 'Screen.' thingies. I didn't post this as an official answer because I'm obviously not 100% sure.
Answer by DMCH · Nov 25, 2012 at 07:17 PM
I found this when looking around. Does a fairly good job of explaining and provides code for different aspect ratios. Might be some use to the next guy!
http://gamedesigntheory.blogspot.ie/2010/09/controlling-aspect-ratio-in-unity.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613084457im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Once more with the Aspect Ratio's 1 Answer
Physic function doesn't work in different resoultions 0 Answers
Why my 2D shadows have low resolution? 0 Answers
gui texture different scale in full screen game. 1 Answer
2D Animation does not start 1 Answer