Created
February 21, 2015 13:21
-
-
Save grifdail/0b908b6097d53681d868 to your computer and use it in GitHub Desktop.
Unity script to move a camera like in Black and White or almost any 3d strategie game.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class CameraControl : MonoBehaviour { | |
public float translateScale = 20; | |
public float rotateScale = 1; | |
public float zoomScale = 100; | |
private Vector3 _lastHitPoint; | |
// Update is called once per frame | |
void Update () { | |
if (Input.GetMouseButton(1)) | |
{ | |
//We are translating | |
Vector3 move = new Vector3(-Input.GetAxis("Mouse X"), 0,-Input.GetAxis("Mouse Y")); | |
Quaternion dir = Quaternion.Euler(0,transform.eulerAngles.y,0); | |
transform.Translate(dir * move *Time.deltaTime * translateScale, Space.World); | |
} | |
if (Input.GetMouseButton(2)) | |
{ | |
//We are rotating; | |
Ray ray = new Ray(transform.position, transform.forward); | |
RaycastHit hit; | |
if (Physics.Raycast(ray, out hit, 50, 1<<LayerMask.NameToLayer("floor"))) | |
{ | |
_lastHitPoint = hit.point; | |
} | |
transform.RotateAround(_lastHitPoint, Vector3.up, Input.GetAxis("Mouse X") * rotateScale); | |
transform.RotateAround(_lastHitPoint, transform.right, -Input.GetAxis("Mouse Y") * rotateScale); | |
} | |
if (Input.GetAxis("Mouse ScrollWheel") != 0) | |
{ | |
//We are zooming | |
transform.Translate(transform.forward * Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomScale, Space.World); | |
} | |
if (transform.position.y < 2) | |
{ | |
transform.position = new Vector3(transform.position.x, 2f, transform.position.z); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment