Created
November 12, 2019 12:27
-
-
Save IshidaGames/ead84ab85ab81fa6f7b36849b96ac1fa 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 Axis : MonoBehaviour | |
{ | |
//X軸の角度を制限するための変数 | |
float angleUp = 60f; | |
float angleDown = -60f; | |
//ユニティちゃんをInspectorで入れる | |
GameObject player; | |
//Main CameraをInspectorで入れる | |
Camera cam; | |
//AxisからのCameraの距離 | |
public float distance = 5f; | |
//AxisとCameraの高さ | |
public float height = 2f; | |
//Cameraのスピード、少ないほど遅く | |
public float attenuate = 5f; | |
//Cameraが回転するスピード | |
[SerializeField] float rotate_speed = 3; | |
//マウススクロールの値を入れる | |
[SerializeField] float scroll; | |
void Start() | |
{ | |
//Playerをタグから取得 | |
player = GameObject.FindGameObjectWithTag("Player"); | |
//Main Cameraを取得 | |
cam = Camera.main; | |
cam.transform.parent = transform; | |
//CameraのAxisに相対的な位置をlocalPositionで指定 | |
cam.transform.localPosition = new Vector3(0, transform.position.y, -distance); | |
//CameraとAxisの向きを最初だけそろえる | |
cam.transform.localRotation = transform.rotation; | |
} | |
void Update() | |
{ | |
// 本来到達しているべきカメラ位置 | |
var camPos = player.transform.position + new Vector3(0, height, 0); | |
transform.position = Vector3.Lerp(transform.position, camPos, Time.deltaTime * attenuate); | |
//マウススクロールの値を入れる | |
scroll = Input.GetAxis("Mouse ScrollWheel"); | |
//Cameraの位置、Z軸にスクロール分を加える | |
cam.transform.localPosition | |
= new Vector3(cam.transform.localPosition.x, | |
cam.transform.localPosition.y, | |
cam.transform.localPosition.z + scroll); | |
//カメラがスクロールでPlayerの前に行かないようにする | |
if(cam.transform.localPosition.z > -1) | |
cam.transform.localPosition = new Vector3(cam.transform.localPosition.x, | |
cam.transform.localPosition.y, | |
-1); | |
//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