Created
August 18, 2019 17:34
-
-
Save m2wasabi/561eb0f86f17ec55d144e84e0f3e65ad to your computer and use it in GitHub Desktop.
Unity用かんたんカメラ動かすやつ
This file contains hidden or 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
// These codes are licensed under CC0. | |
// http://creativecommons.org/publicdomain/zero/1.0/deed.ja | |
using UnityEngine; | |
/// <summary> | |
/// The camera added this script will follow the specified object. | |
/// The camera can be moved by left mouse drag and mouse wheel. | |
/// </summary> | |
[ExecuteInEditMode, DisallowMultipleComponent] | |
public class FollowingCamera : MonoBehaviour | |
{ | |
public GameObject target; // an object to follow | |
public Vector3 offset; // offset form the target object | |
[SerializeField] private float distance = 1.0f; // distance from following object | |
[SerializeField] private float polarAngle = 90.0f; // angle with y-axis | |
[SerializeField] private float azimuthalAngle = 90.0f; // angle with x-axis | |
[SerializeField] private float minDistance = 0.01f; | |
[SerializeField] private float maxDistance = 7.0f; | |
[SerializeField] private float minPolarAngle = 5.0f; | |
[SerializeField] private float maxPolarAngle = 170.0f; | |
[SerializeField] private Vector2 mouseTiltSensitivity = new Vector2( 5.0f, 5.0f); | |
[SerializeField] private float scrollSensitivity = 0.5f; | |
[SerializeField] private Vector2 mouseMoveSensitivity = new Vector2(0.05f, 0.05f); | |
void LateUpdate() | |
{ | |
if (Input.GetMouseButton(1)) { | |
updateAngle(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); | |
} | |
if (Input.GetMouseButton(2)) { | |
updateOffset(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); | |
} | |
updateDistance(Input.GetAxis("Mouse ScrollWheel")); | |
var lookAtPos = target.transform.position + offset; | |
updatePosition(lookAtPos); | |
transform.LookAt(lookAtPos); | |
} | |
void updateAngle(float x, float y) | |
{ | |
x = azimuthalAngle - x * mouseTiltSensitivity.x; | |
azimuthalAngle = Mathf.Repeat(x, 360); | |
y = polarAngle + y * mouseTiltSensitivity.y; | |
polarAngle = Mathf.Clamp(y, minPolarAngle, maxPolarAngle); | |
} | |
void updateOffset(float x, float y) | |
{ | |
offset += transform.rotation * new Vector3(- x * mouseMoveSensitivity.x, - y * mouseMoveSensitivity.y, 0); | |
} | |
void updateDistance(float scroll) | |
{ | |
scroll = distance - scroll * scrollSensitivity; | |
distance = Mathf.Clamp(scroll, minDistance, maxDistance); | |
} | |
void updatePosition(Vector3 lookAtPos) | |
{ | |
var da = azimuthalAngle * Mathf.Deg2Rad; | |
var dp = polarAngle * Mathf.Deg2Rad; | |
transform.position = new Vector3( | |
lookAtPos.x + distance * Mathf.Sin(dp) * Mathf.Cos(da), | |
lookAtPos.y + distance * Mathf.Cos(dp), | |
lookAtPos.z + distance * Mathf.Sin(dp) * Mathf.Sin(da)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment