Skip to content

Instantly share code, notes, and snippets.

@peroon
Created June 12, 2015 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peroon/9edce7942fc29c663f9a to your computer and use it in GitHub Desktop.
Save peroon/9edce7942fc29c663f9a to your computer and use it in GitHub Desktop.
Orbit Camera マウスでグリグリできる。タッチは未検証
using UnityEngine;
using System.Collections;
// http://twiik.net/articles/realtime-reflections-in-unity-5
public class OrbitCamera : MonoBehaviour {
public Transform target;
public float rotateSpeed = 3.0f;
float distance;
float _x;
float _y;
float timer;
float scrollWheel;
void Start () {
distance = 6;
_y = 45;
RotateCamera();
transform.LookAt(target);
}
void Update() {
scrollWheel = Input.GetAxis("Mouse ScrollWheel");
timer += Time.deltaTime;
if (scrollWheel > 0 && timer > 0.01f) {
timer = 0;
distance -= 0.5f;
if (distance <= 1) {
distance = 1;
}
RotateCamera();
}
if (scrollWheel < 0 && timer > 0.01f) {
timer = 0;
distance += 0.5f;
if (distance >= 10) {
distance = 10;
}
RotateCamera();
}
if (Input.GetMouseButton(0)) {
_x += Input.GetAxis("Mouse X") * rotateSpeed;
_y -= Input.GetAxis("Mouse Y") * rotateSpeed;
RotateCamera();
}
}
void RotateCamera() {
Quaternion rotation = Quaternion.Euler(_y, _x, 0);
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment