Created
June 22, 2019 09:30
-
-
Save IshidaGames/30dbdd415b5e59ec89b3a7cb555bec14 to your computer and use it in GitHub Desktop.
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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class CameraController : MonoBehaviour | |
{ | |
//X軸の角度を制限するための変数 | |
float angleUp = 60f; | |
float angleDown = -60f; | |
GameObject player; | |
GameObject cam; | |
//Cameraが回転するスピード | |
[SerializeField] float rotate_speed = 3; | |
//Axisの位置を指定する変数 | |
[SerializeField] Vector3 axisPos; | |
//Cameraの位置を調整する変数 | |
[SerializeField] Vector3 cameraPos; | |
//マウススクロールの値を入れる | |
[SerializeField] float scroll; | |
//マウスホイールの値を保存 | |
[SerializeField] float scrollLog; | |
void Start() | |
{ | |
//Playerタグのオブジェクトを取得 | |
player = GameObject.FindGameObjectWithTag("Player"); | |
//Main Cameraをオブジェクトとして取得 | |
cam = Camera.main.gameObject; | |
//Main Cameraをこのオブジェクトの子にする | |
cam.transform.parent = this.transform; | |
//CameraのAxisに相対的な位置をlocalPositionで指定 | |
cam.transform.localPosition = new Vector3(0, 0.3f, -1) + cameraPos; | |
//CameraとAxisの向きを最初だけそろえる | |
cam.transform.localRotation = transform.rotation; | |
} | |
void Update() | |
{ | |
//Axisの位置をPlayerの位置+axisPosで決める | |
transform.position = player.transform.position + axisPos; | |
//マウススクロールの値を入れる | |
scroll = Input.GetAxis("Mouse ScrollWheel"); | |
//マウススクロールの値は動かさないと0になるのでここで保存する | |
scrollLog += Input.GetAxis("Mouse ScrollWheel"); | |
//Cameraの位置、Z軸にスクロール分を加える | |
cam.transform.localPosition | |
= new Vector3(cam.transform.localPosition.x, | |
cam.transform.localPosition.y, | |
cam.transform.localPosition.z + scroll); | |
//Cameraの角度にマウスからとった値を入れる | |
transform.eulerAngles += new Vector3( | |
Input.GetAxis("Mouse Y") * rotate_speed, | |
Input.GetAxis("Mouse X") * rotate_speed | |
, 0); | |
//X軸の角度 | |
float angleX = transform.eulerAngles.x; | |
//X軸の値を180度超えたら360引くことで制限しやすくする | |
if (angleX >= 180) | |
{ | |
angleX = angleX - 360; | |
} | |
//Mathf.Clamp(値、最小値、最大値)でX軸の値を制限する | |
transform.eulerAngles = new Vector3( | |
Mathf.Clamp(angleX, angleDown, angleUp), | |
transform.eulerAngles.y, | |
transform.eulerAngles.z | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment