Skip to content

Instantly share code, notes, and snippets.

@nkjzm
Created September 9, 2019 15:18
Show Gist options
  • Save nkjzm/c7a4c645f20593a5ce38bc91776cbeb0 to your computer and use it in GitHub Desktop.
Save nkjzm/c7a4c645f20593a5ce38bc91776cbeb0 to your computer and use it in GitHub Desktop.
Editor上でCameraをキーボード操作するためのコンポーネント
using UnityEngine;
/// <summary>
/// Editor上でCameraをキーボード操作するためのコンポーネント
/// </summary>
public class EditorCamera : MonoBehaviour
{
const float Angle = 30f;
const float Speed = 0.25f;
bool IsShift { get { return Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift); } }
bool shouldRotateLeft { get { return !IsShift && (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A)); } }
bool shouldRotateRight { get { return !IsShift && (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D)); } }
bool shouldMoveforwad { get { return !IsShift && (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W)); } }
bool shouldMoveBack { get { return !IsShift && (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S)); } }
bool shouldMoveLeft { get { return IsShift && (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A)); } }
bool shouldMoveRight { get { return IsShift && (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D)); } }
bool shouldMoveUp { get { return IsShift && (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W)); } }
bool shouldMoveDown { get { return IsShift && (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S)); } }
void Update()
{
// [←|A]キーで左回転
if (shouldRotateLeft) { transform.Rotate(0, -Angle, 0); }
// [→|D]キーで右回転
if (shouldRotateRight) { transform.Rotate(0, Angle, 0); }
// [↑|W]キーで前方移動
if (shouldMoveforwad) { transform.position += transform.forward.normalized * Speed; }
// [↓|S]キーで後方移動
if (shouldMoveBack) { transform.position -= transform.forward.normalized * Speed; }
// [Shift+(←|A)]キーで左方移動
if (shouldMoveLeft) { transform.position -= transform.right.normalized * Speed; }
// [Shift+(→|D)]キーで右方移動
if (shouldMoveRight) { transform.position += transform.right.normalized * Speed; }
// [Shift+(↑|W)]キーで上方移動
if (shouldMoveUp) { transform.position += transform.up.normalized * Speed; }
// [Shift+(↓|S)]キーで下方移動
if (shouldMoveDown) { transform.position -= transform.up.normalized * Speed; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment