Created
January 12, 2014 11:06
-
-
Save TAK-EMI/8383314 to your computer and use it in GitHub Desktop.
*新しいの作りました。https://gist.github.com/TAK-EMI/d67a13b6f73bed32075d Unityでメタセコイアのようにカメラを操作できるようにするためのスクリプト。使用してるカメラにaddすればOK。マウスの右ドラッグでカメラ回転。中ドラッグで移動。ホイールでカメラが前後に移動。質問等あればこちらまでどうぞ。https://twitter.com/TAK_EMI
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; | |
namespace TAK_CameraController | |
{ | |
enum MouseButtonDown | |
{ | |
MBD_LEFT = 0, | |
MBD_RIGHT, | |
MBD_MIDDLE, | |
}; | |
public class CameraController : MonoBehaviour | |
{ | |
[SerializeField] | |
private Vector3 focus = Vector3.zero; | |
[SerializeField] | |
private GameObject focusObj = null; | |
private Vector3 oldPos; | |
void setupFocusObject(string name) | |
{ | |
GameObject obj = this.focusObj = new GameObject(name); | |
obj.transform.position = this.focus; | |
obj.transform.LookAt(this.transform.position); | |
return; | |
} | |
void Start () | |
{ | |
if (this.focusObj == null) | |
this.setupFocusObject("CameraFocusObject"); | |
Transform trans = this.transform; | |
transform.parent = this.focusObj.transform; | |
trans.LookAt(this.focus); | |
return; | |
} | |
void Update () | |
{ | |
this.mouseEvent(); | |
return; | |
} | |
void mouseEvent() | |
{ | |
float delta = Input.GetAxis("Mouse ScrollWheel"); | |
if (delta != 0.0f) | |
this.mouseWheelEvent(delta); | |
if (Input.GetMouseButtonDown((int)MouseButtonDown.MBD_LEFT) || | |
Input.GetMouseButtonDown((int)MouseButtonDown.MBD_MIDDLE) || | |
Input.GetMouseButtonDown((int)MouseButtonDown.MBD_RIGHT)) | |
this.oldPos = Input.mousePosition; | |
this.mouseDragEvent(Input.mousePosition); | |
return; | |
} | |
void mouseDragEvent(Vector3 mousePos) | |
{ | |
Vector3 diff = mousePos - oldPos; | |
if (Input.GetMouseButton((int)MouseButtonDown.MBD_LEFT)) | |
{ | |
}else if (Input.GetMouseButton((int)MouseButtonDown.MBD_MIDDLE)) | |
{ | |
if (diff.magnitude > Vector3.kEpsilon) | |
this.cameraTranslate(-diff / 10.0f); | |
}else if (Input.GetMouseButton((int)MouseButtonDown.MBD_RIGHT)) | |
{ | |
if (diff.magnitude > Vector3.kEpsilon) | |
this.cameraRotate(new Vector3(diff.y, diff.x, 0.0f)); | |
} | |
this.oldPos = mousePos; | |
return; | |
} | |
public void mouseWheelEvent(float delta) | |
{ | |
Vector3 focusToPosition = this.transform.position - this.focus; | |
Vector3 post = focusToPosition * (1.0f + delta); | |
if (post.magnitude > 0.01) | |
this.transform.position = this.focus + post; | |
return; | |
} | |
void cameraTranslate(Vector3 vec) | |
{ | |
Transform focusTrans = this.focusObj.transform; | |
vec.x *= -1; | |
focusTrans.Translate(Vector3.right * vec.x); | |
focusTrans.Translate(Vector3.up * vec.y); | |
this.focus = focusTrans.position; | |
return; | |
} | |
public void cameraRotate(Vector3 eulerAngle) | |
{ | |
Transform focusTrans = this.focusObj.transform; | |
focusTrans.localEulerAngles = focusTrans.localEulerAngles + eulerAngle; | |
this.transform.LookAt(this.focus); | |
return; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment