0

I have a top-down perspective camera view of an object in my scene.

I'm exporting for WebGL, and I want to start off the scene with a top-down view of the object, but I need it to remain the same size in screen pixels no-matter the width/height of the Unity player on the web page.

I want to achieve this by moving the camera up/down (along the y axis), but I'm struggling with the maths needed to calculate the correct y position of the camera.

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

public class MainCamera : MonoBehaviour {

    private Camera cam;
    public Transform target;
    public float targetPixelHeight = 150;

    void Start () {
        cam = GetComponent<Camera>();
    }

    void Update () {

        float newY = /*unknown calculation involving camera position and targetPixelHeight*/;
        transform.position = new Vector3(transform.position.x, newY, transform.position.z);
    }

}
gman
  • 100,619
  • 31
  • 269
  • 393
bbeckford
  • 4,467
  • 6
  • 34
  • 48
  • If you change the camera's size, wouldn't it keep the amount of pixels the same? So depending on the screen size, adjust the camera's size adequately for it. – Alox Jul 21 '17 at 13:28
  • What do you mean by camera size? – bbeckford Jul 21 '17 at 14:18
  • I think you could use PlayerSettings.defaultScreenWidth = 1080; PlayerSettings.defaultScreenWidth = 1920; where width and height are the resolutions of the screen it's on. – Alox Jul 21 '17 at 14:35
  • I'm not sure I follow, I can't predict what size the player will be on the web page, but I want to keep the size of my object in pixels consistent nomatter what. – bbeckford Jul 21 '17 at 15:10
  • If you can't find the screen's resolution then there wouldn't really be any way to have the pixels be constant regardless of the screen size? You need a value regarding that to be able to calculate anything regarding appropriate scaling... – Alox Jul 21 '17 at 15:40
  • Yes you can get the screen size at any time with Screen.width or Screen.height – bbeckford Jul 22 '17 at 08:55

0 Answers0