Skip to content

Instantly share code, notes, and snippets.

@bdecarne
Last active November 5, 2015 18:01
Show Gist options
  • Save bdecarne/8c4c95d97788e7dc1f54 to your computer and use it in GitHub Desktop.
Save bdecarne/8c4c95d97788e7dc1f54 to your computer and use it in GitHub Desktop.
Unity : CameraNavigation
using UnityEngine;
using System.Collections;
public class CameraLook : MonoBehaviour {
float minFov;
float maxFov;
float sensitivity;
int boundary; // distance from edge scrolling starts
int speed;
private int screenWidth;
private int screenHeight;
// Use this for initialization
void Start()
{
minFov = 15f;
maxFov = 90f;
sensitivity = 50f;
boundary = 50;
speed = 20;
screenWidth = Screen.width;
screenHeight = Screen.height;
}
// Update is called once per frame
void Update()
{
float fov = Camera.main.fieldOfView;
fov -= Input.GetAxis("Mouse ScrollWheel") * sensitivity;
fov = Mathf.Clamp(fov, minFov, maxFov);
Camera.main.fieldOfView = fov;
Vector3 position = transform.position;
if (Input.mousePosition.x > screenWidth - boundary && Input.mousePosition.x < screenWidth)
{
position.x += speed * Time.deltaTime;
}
if (Input.mousePosition.x < 0 + boundary && Input.mousePosition.x >= 0)
{
position.x -= speed * Time.deltaTime;
}
if (Input.mousePosition.y > screenHeight - boundary && Input.mousePosition.y < screenHeight)
{
position.z += speed * Time.deltaTime;
}
if (Input.mousePosition.y < 0 + boundary && Input.mousePosition.y >= 0)
{
position.z -= speed * Time.deltaTime;
}
transform.position = position;
}
void OnGUI()
{
GUI.Box(new Rect((Screen.width / 2) - 140, 5, 280, 25), "Mouse Position = " + Input.mousePosition);
GUI.Box(new Rect((Screen.width / 2) - 70, Screen.height - 30, 140, 25), "Mouse X = " + Input.mousePosition.x);
GUI.Box(new Rect(5, (Screen.height / 2) - 12, 140, 25), "Mouse Y = " + Input.mousePosition.y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment