# How do I get the size of a Plane in pixels and set it multiplied by some factor to be the size of a Sphere?

I am doing an attempt to make a 3D snake game, as an experiment but I am still a beginner. I have a Sphere that I wish to dynamically resize to be, let's say, the 20th part of the width of this Plane. How can I do this? I thought to find the size of the Plane first. I also thought to use Screen.width and Screen.height because in my actual project the Plane is almost the size of the screen, but from what I found the Transform class does not have a way to resize it to a specific dimension in pixels.

Using the code below I do not get this work, but I also see that the Sphere becomes a black circle because of the only instruction in the Start method.

# Existing code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// Attached to the Plane
public class SphereResizer : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
// what should I put here?
GameObject.Find("Sphere").transform.localScale = new Vector3(1f, 1f);
}

// Update is called once per frame
void Update()
{
}
}


# Screenshots

Here is the GitHub repository.

Thank you.

• When you are using a perspective camera, then the size in pixels of an object depends on the distance to the camera and the camera's field of view. Jan 13, 2020 at 15:33
• @Philipp How can I get the ratio between the width of a Transform and the width of another Transform, relative to a camera? Should I ask this in a new question? Thank you. Jan 13, 2020 at 17:54
• @silivubogan I think you are approaching that problem from the wrong direction. Why don't you just resize the plane to a size that's convenient to work with and then adjust the position of the camera so it has the right size on the screen? Jan 13, 2020 at 20:11
• @Philipp I know I can do this in the Unity editor but I have to do this programmatically. In this 3D snake game I have the snake head (and the future snake body parts) that should be dynamically scaled for when the dimensions of the level grid (the number of cells on the width of the Plane or on the height of the Plane) change (I could offer this option to the player in future.). Thank you. Jan 14, 2020 at 9:38

As this question is kinda 2 questions in one, I'm going to answer the one that I think is the root of your problem (scale sphere relative to plane). Because if I read this correctly, the pixel part sounds like you only want to do that because you couldn't find the solution to your original problem.

In Unity, a position is based on 'units'. a 'unit' doesn't have a set real world scale, it is just a number. As in, if you want a unit to be a mm, you pretend it is a mm. If you want it to be an inch, you pretend it to be an inch. (if you have UI with rendermode set to screen space overlay, it pretends 1 unit is 1 pixel). Note however that the physics engine is by default configured so that 1 unit == 1 meter in the real world. The grid that is drawn in the scene view is 1x1 unit, 10x10 units or 100x100 units (depending on zoom level).

How much pixels this one unit actually is depends on the camera used to render it, and there could even be 2 cameras rendering the same 3D object, resulting in the same object to be 2 different sizes in pixels on 2 different cameras.

The default cube and sphere, with a scale of [1,1,1] take up 1 unit in each axis.

The default plane, with a scale of [1,1,1] and not rotated takes up 10 units in x and z. And well, its a plane, so basically nothing in y...

So by default 10 spheres fit on a plane, so if you want to fit 20 spheres on a plane, you'd need to set the sphere scale to [0.5, 0.5, 0.5].

Also note, that you are giving the Vector3 constructor 2 parameters instead of 3. Meaning the z defaults to 0, resulting in your black sphere. So if you'd set the z aswell like this:

GameObject.Find("Sphere").transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);


it should work better.

Also, you'd might want to consider using a quad instead of a plane... As that one by default takes up 1 unit in x/y. Which might just make it more intuitive for you...

• I have a camera and I wish to resize the sphere to have a radius relative to the plane behind it. The plane can get bigger or smaller. I could compute the size of the sphere in the same place where I compute the size of the plane. The problem I'm facing is that when I compute the size of the plane I must move the camera so that it sees the entire plane. I am working with the field of view of the camera and its position but I do not know how to do it. Should I ask a separate question? I think it is simple math... something with sin and cos. Thank you very much! Jan 14, 2020 at 14:56
• If you want to place the camera so that it always fits a plane, I'd suggest asking a new question, as then other people who want to do the same can find it more easy. (but first check if someone else asked that question already). To have the sphere have a radius relative to the plane behind it is in my answer I think. Giving the sphere a scale of [10,10,10] gives it the same size as the plane. Where the formula for scale would be: float scale = 10/numberOfSpheresWantedOnPlane; and then GameObject.Find("Sphere").transform.localScale = new Vector3(scale, scale, scale);. Jan 14, 2020 at 15:15
• I asked a new question here. Thank you. Jan 15, 2020 at 10:31