企業研修個人開発 2020年11月1日

C# by Unity

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraChange : MonoBehaviour
{
public Camera[] cameras;
public int zoomNum;
public float FieldOfViewNum;
public float RotationXNum;
public float RotationYNum;
public int cameraNum = 0;
public float speed;

//複数台のカメラを選ぶ機能
public void ChangeCamera(int num)
{
for (int i = 0; i < cameras.Length; i++)
{
if (i == num)
{
cameras[i].enabled = true;

}
else
{
cameras[i].enabled = false;
}
}
//各カメラのデータを取得し変数に代入
cameraNum = num;
FieldOfViewNum = cameras[cameraNum].fieldOfView;
Quaternion quaternion = cameras[cameraNum].transform.rotation;

//Quaternionの x, yをfloatの変数に代入
RotationXNum = quaternion.eulerAngles.x;
RotationYNum = quaternion.eulerAngles.y;

}

//ズーム機能
public void CameraZoom(int whichOne)
{
if (whichOne == 0)
{
FieldOfViewNum += speed;
}
else
{
FieldOfViewNum -= speed;
}
cameras[cameraNum].fieldOfView = Mathf.Clamp(FieldOfViewNum, 0.1f, 170f);
}
//左右にカメラを動かす機能
public void CameraRotationX(int whichOne)
{
if (whichOne == 0)
{
RotationXNum += speed;
}
else
{
RotationXNum -= speed;
}
RotationUpDate();
}
//上下にカメラを動かす機能
public void CameraRotationY(int whichOne)
{
if (whichOne == 0)
{
RotationYNum += speed;
}
else
{
RotationYNum -= speed;
}
RotationUpDate();
}
//カメラのtransformのデータを更新
public void RotationUpDate()
{
cameras[cameraNum].transform.rotation = Quaternion.Euler(RotationXNum, RotationYNum, 0);
}
}

この記事を書いた人