Reputation:
In my Unity3D project, I have a board created dynamically (using c#) and located on the screen as follow:
0 < x < 12
0 < z < 12
y = -1
How can I make the camera focusing on the board and make it centered for different screen resolutions and for different platforms?
Upvotes: 2
Views: 2180
Reputation: 804
Here's an example :
using UnityEngine;
using System.Collections;
/**
* This class attempts to force VERT- Field of view scaling.
* By default, Unity uses the HOR+ technique.
*
* http://en.wikipedia.org/wiki/Field_of_view_in_video_games#Scaling_methods
*/
[ExecuteInEditMode]
[RequireComponent (typeof(Camera))]
public class VertMinusCameraFOV : MonoBehaviour {
public float designTimeVerticalFieldOfView = 60;
public int designTimeWidth = 1280; // default screen width
public int designTimeHeight = 720; // default screen height
private float hFOVInRads;
private int prevWidth;
private int prevHeight;
void Start () {
prevWidth = designTimeWidth;
prevHeight = designTimeHeight;
float aspectRatio = (float) designTimeWidth / (float) designTimeHeight;
float vFOVInRads = designTimeVerticalFieldOfView * Mathf.Deg2Rad;
hFOVInRads = 2f * Mathf.Atan( Mathf.Tan(vFOVInRads/2f) * aspectRatio);
}
void Update () {
if (Screen.width != prevWidth || Screen.height != prevHeight) { // capture screen ratio changes
float aspectRatio = (float) Screen.width / (float) Screen.height;
float vFOVInRads = 2f * Mathf.Atan( Mathf.Tan(hFOVInRads/2f) / aspectRatio );
Debug.Log("Screen resolution change. Recomputing aspect ratio (" + aspectRatio + ") and field of view (" + vFOVInRads*Mathf.Rad2Deg + ")");
foreach (Camera cam in GameObject.FindObjectsOfType(typeof(Camera))) {
cam.fieldOfView = vFOVInRads * Mathf.Rad2Deg;
}
}
}
}
Upvotes: 1
Reputation: 12243
Can you try a SmoothLookAt or transform.LookAt? You can put both on your camera, and specify your board as your "target". You can also do an iTween to make it appear that you are smoothing into looking at a specified target.
private var target : Transform;
var damping = 6.0;
var smooth = true;
@script AddComponentMenu("Camera-Control/Smooth Look At")
function LateUpdate () {
if (target) {
if (smooth)
{
// Look at and dampen the rotation
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
else
{
// Just lookat
transform.LookAt(target);
}
}
}
function Start () {
// Make the rigid body not change rotation
if (target == null)
target = GameObject.Find("Board_Name").transform;
if (GetComponent.<Rigidbody>())
GetComponent.<Rigidbody>().freezeRotation = true;
}
With iTween you can use
iTween.MoveUpdate(gameObject, iTween.Hash("position",board.position,"time",2));
iTween.LookUpdateModified(gameObject,iTween.Hash("looktarget",board.position,"time",2));
There is also a few other camera scripts you could potentially use.
I have also done a mouse Pan Zoom type script a while back. Keep in mind minZoom and maxZoom have to be in between 0 and 180 but you can make it more granular, attach this to Camera and you can control with scroll wheel. I also have touch scripts for Zoom and mobile. This will allow you to zoom in and out.:
if (Input.GetAxis("Mouse ScrollWheel")!=0) {
camera.fieldOfView += Input.GetAxis("Mouse ScrollWheel");
camera.fieldOfView = Mathf.Clamp(camera.fieldOfView, minZoom, maxZoom);
}
Upvotes: 0