Question by
nebula1 · Jul 21, 2017 at 12:35 AM ·
camerawebglperspective camera
Keep GameObject size in pixels on screen by zooming camera
Hi all, 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;
void Start () {
cam = GetComponent<Camera>();
}
void Update () {
float newY = /*unknown calculation*/;
transform.position = new Vector3(transform.position.x, newY, transform.position.z);
}
}
Comment
Your answer
Follow this Question
Related Questions
Webgl Multi Scale AO not working in build 2 Answers
Orthographic Camera for objects but perspective for GUI? 1 Answer
Very strange error when building 0 Answers
WebGL colors glitching/ washed out 0 Answers
Shadow rendering distance HTML5 1 Answer