0

I have 4 sprites with colliders, I want to auto position and scale them to the bottom of the screen evenly so they don't over lap and they are not off screen at all. I can not do this in canvas it needs to be done as gameObjects.

I also am trying to get each Sprits height to be 1/4th-1/5th depending on how it looks, that's why the code is divided by 4 down below.

How do I get them to position on the bottome and side by side?

public class AutoPosition : MonoBehaviour {

public Sprite [] goals;

public float width = Screen.width / 4;
public float height = Screen.height / 4;
// Use this for initialization
void Start () {
    for (int i = 0; i < goals.Length; i++) {
        goals[i]
    }
}
shane
  • 342
  • 1
  • 5
  • 28
  • Why can't you use a canvas? (GameObjects are GameObjects also with a Canvas as parent). And does the camera move or stay static? – derHugo Oct 13 '18 at 04:40
  • 1
    look https://stackoverflow.com/questions/52789304/canvas-with-box-colliders-confusion?noredirect=1#comment92499804_52789304 the camera is static but screen sizes change @derHugo – shane Oct 13 '18 at 05:10

1 Answers1

1

You can use SpriteRender for the images. And position them inside of a parent GameObject. Than it is enough to simply scale and position that one Parent GameObject correctly (similar to the canvas but with normal Transform components).

public class applySize : MonoBehaviour 
{
    private void Apply()
    {
        // Get the main camera position
        var cameraPosition = Camera.main.transform.position;

        // This makes the parent GameObject "fit the screen size"
        float height;
        if (Camera.main.orthographic)
        {
            // Camera projection is orthographic
            height = 2 * Camera.main.orthographicSize;
        }
        else
        {
            // Camera projection is perspective
            height = 2 * Mathf.Tan(0.5f * Camera.main.fieldOfView * Mathf.Deg2Rad) * Mathf.Abs(cameraPosition.z - transform.position.z);
        }

        var width = height * Camera.main.aspect;

        transform.localScale = new Vector3(width, height,1);
        transform.position = cameraPosition - new Vector3(0,height*.375f, cameraPosition.z);

        // Since the 4 images are childs of the parent GameObject there is no need
        // place or scale them separate. It is all done with placing this parent object
    }

    private void Start()
    {
        Apply();
    }
}

Using the following Scene setup

enter image description here

The X positions of the Sprites simply are

  • image1: - width * 1.5;
  • image2: - width * 0.5;
  • image3: width * 0.5;
  • image4: width * 1.5;

and for the four SpriteRenderers

enter image description here

and the colliders

enter image description here

Result

(with an additional call in Update)

enter image description here

I made the Parent position stay on Z = 0. You can change this according to your needs. This way the colliders should now be able to interact with other objects.

derHugo
  • 83,094
  • 9
  • 75
  • 115