Showing posts with label Camera. Show all posts
Showing posts with label Camera. Show all posts

Unity Horizontal Field Of View for Camera

Unity3D Horizontal Field Of View for Camera

In Unity3d you can change only vertical field of view for a camera by default. This means whenever you change height unity will automatically scale environment. But if you change width unity will just add some space (or decrease) to left and right side of the screen (no content resizing).

If you want to have opposite behavior, so when you increase width all content will be scaled and if you increase height only space added to the view, you need to modify camera fieldOfView field.

Here is a simple component that do this:
public class HorizontalFOV : MonoBehaviour {
public float fieldOfView = 60;
  
void Update () {
    camera.fieldOfView = fieldOfView / ((float)camera.pixelWidth / camera.pixelHeight);
    }
}

You can also modify it a bit to have the same view for default aspect ration:
public class HorizontalFOV : MonoBehaviour {
public float fieldOfView = 60;
  
void Update () {
    camera.fieldOfView = fieldOfView * (16f/9f) / ((float)camera.pixelWidth / camera.pixelHeight);
    }
}

Unity3D Horizontal Field Of View for Camera

Unity3D Camera Shake Script

Unity3D Camera Shake Free Script

After much Googling for how to do a camera shaking effect in Unity, I decided to write my own small script to accomplish the effect. It combines both positional movement with rotational movement to simulate closely an actual camera shake.
The two key variables are:

shake_intensity

Shake_intensity determines the initial intensity of the shaking — how much variance to allow in the camera position.

shake_decay

Shake_decay controls is the amount that shake_intensity is decremented each update. It determines if the shake is long or short.

JavaScript
var originPosition:Vector3;
var originRotation:Quaternion;
var shake_decay: float;
var shake_intensity: float;;
 
function OnGUI () {
   if (GUI.Button (Rect (20,40,80,20), "Shake")) {
      Shake();
   }
}
 
function Update(){
   if(shake_intensity > 0){
      transform.position = originPosition + Random.insideUnitSphere * shake_intensity;
      transform.rotation = Quaternion(
      originRotation.x + Random.Range(-shake_intensity,shake_intensity)*.2,
      originRotation.y + Random.Range(-shake_intensity,shake_intensity)*.2,
      originRotation.z + Random.Range(-shake_intensity,shake_intensity)*.2,
      originRotation.w + Random.Range(-shake_intensity,shake_intensity)*.2);
      shake_intensity -= shake_decay;
   }
}
 
function Shake(){
   originPosition = transform.position;
   originRotation = transform.rotation;
   shake_intensity = .3;
   shake_decay = 0.002;
}

C#
using UnityEngine;
using System.Collections;
public class cameraShake : MonoBehaviour
{
   private Vector3 originPosition;
   private Quaternion originRotation;
   public float shake_decay;
   public float shake_intensity;
 
   void OnGUI (){
      if (GUI.Button (new Rect (20,40,80,20), "Shake")){
         Shake ();
      }
   }
 
   void Update (){
      if (shake_intensity > 0){
         transform.position = originPosition + Random.insideUnitSphere * shake_intenity;
         transform.rotation = new Quaternion(
         originRotation.x + Random.Range (-shake_intensity,shake_intensity) * .2f,
         originRotation.y + Random.Range (-shake_intensity,shake_intensity) * .2f,
         originRotation.z + Random.Range (-shake_intensity,shake_intensity) * .2f,
         originRotation.w + Random.Range (-shake_intensity,shake_intensity) * .2f);
         shake_intensity -= shake_decay;
      }
   }
 
   void Shake(){
      originPosition = transform.position;
      originRotation = transform.rotation;
      shake_intensity = .3f;
   shake_decay = 0.002f;
   }
}

Unity3D Camera Shake Free Script

Unity Move, Zoom and Rotate Camera

Unity Move, Zoom and Rotate Camera

Move: It is same as moving character. Just use keyboard buttons and move camera with a float speed variable.
private float speed = 2.0f;
void Update () {
 
  if (Input.GetKey(KeyCode.RightArrow)){
   transform.position += Vector3.right * speed * Time.deltaTime;
  }
  if (Input.GetKey(KeyCode.LeftArrow)){
   transform.position += Vector3.left * speed * Time.deltaTime;
  }
  if (Input.GetKey(KeyCode.UpArrow)){
   transform.position += Vector3.forward * speed * Time.deltaTime;
  }
  if (Input.GetKey(KeyCode.DownArrow)){
   transform.position += Vector3.back * speed * Time.deltaTime;
  }
 }

Zoom: To zoom with scroll wheel, we need scroll wheel input to zoom in or zoom out.
private float zoomSpeed = 2.0f;
 
 void Update () {
 
  float scroll = Input.GetAxis("Mouse ScrollWheel");
  transform.Translate(0, scroll * zoomSpeed, scroll * zoomSpeed, Space.World);
 }

Rotate: I will give an example with mouse button pressed but you can change it with any button or none. Just change or delete if(Input.GetMouseButton(0)).

We need maximum and minimum values for the axes and assign sensitivities.
public float minX = -360.0f;
 public float maxX = 360.0f;
 
 public float minY = -45.0f;
 public float maxY = 45.0f;
 
 public float sensX = 100.0f;
 public float sensY = 100.0f;
 
 float rotationY = 0.0f;
 float rotationX = 0.0f;
 
 void Update () {
 
  if (Input.GetMouseButton (0)) {
   rotationX += Input.GetAxis ("Mouse X") * sensX * Time.deltaTime;
   rotationY += Input.GetAxis ("Mouse Y") * sensY * Time.deltaTime;
   rotationY = Mathf.Clamp (rotationY, minY, maxY);
   transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
  }
 }

Unity Move, Zoom and Rotate Camera