Last active
May 4, 2018 09:27
-
-
Save todorok1/298945046a94caad9e16a93e95b21383 to your computer and use it in GitHub Desktop.
Unityチュートリアル・コースアウト時の演出を実装するスクリプト
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; | |
using UnityEngine.UI; | |
public class CameraController : MonoBehaviour { | |
// 『Sphere』オブジェクトへの参照 | |
[SerializeField] | |
GameObject player; | |
// 『Slider』オブジェクトへの参照 | |
[SerializeField] | |
GameObject sliderObject; | |
// 『ZoomText』オブジェクトへの参照 | |
[SerializeField] | |
GameObject zoomTextObject; | |
// 『Sphere』オブジェクトと『MainCamera』オブジェクトの距離 | |
Vector3 offset; | |
// Sliderコンポーネントの参照 | |
Slider zoomSlider; | |
// Textコンポーネントの参照 | |
Text zoomText; | |
// 拡大率のテキストで変数以外の部分を定義 | |
string zoomTextPrefix = "Magnification : "; | |
string zoomTextSuffix = "%"; | |
// 拡大率の最小値と最大値 | |
const int OffsetMin = 50; | |
const int OffsetMax = 150; | |
// カメラの拡大率(%) | |
[SerializeField, Range(OffsetMin, OffsetMax)] | |
int magnify = 100; | |
// カメラがボールに追随するかのフラグ | |
bool isTracing = true; | |
void Start(){ | |
// オフセットを計算する | |
offset = gameObject.transform.position - player.transform.position; | |
// 参照を取得 | |
zoomSlider = sliderObject.GetComponent<Slider>(); | |
zoomText = zoomTextObject.GetComponent<Text>(); | |
} | |
void LateUpdate(){ | |
// カメラの拡大率に応じたオフセットを取得 | |
Vector3 magnifiedOffset = GetMagnifiedOffset(); | |
if (isTracing){ | |
// 『Sphere』オブジェクトとオフセットからカメラの現在位置を計算 | |
gameObject.transform.position = player.transform.position + magnifiedOffset; | |
} | |
} | |
Vector3 GetMagnifiedOffset(){ | |
// 規格化されたオフセットを取得 | |
Vector3 normalizedOffset = offset.normalized; | |
// 『Sphere』オブジェクトとカメラの距離を取得 | |
float offsetDistance = offset.magnitude; | |
// offsetDistanceに拡大率をかけて補正後の距離を取得 | |
float magnifiedDistance = offsetDistance * (200f - magnify) / 100f; | |
// 規格化されたベクトルと拡大後の距離からオフセットを返す | |
Vector3 magnifiedOffset = magnifiedDistance * normalizedOffset; | |
return magnifiedOffset; | |
} | |
public void OnChangedMagnifyValue(){ | |
// Sliderの値を拡大率に反映 | |
magnify = (int)zoomSlider.value; | |
// ZoomTextに文字列を設定 | |
zoomText.text = zoomTextPrefix + magnify.ToString() + zoomTextSuffix; | |
} | |
public void SetTracingState(bool isActive){ | |
// 外部からisTracingフラグをセットする | |
isTracing = isActive; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment