Created
July 25, 2024 12:10
-
-
Save yoh7686/6eb35e03cec05dfc7f9b994cae593fc3 to your computer and use it in GitHub Desktop.
Plateau SDK Toolkitを使った花火大会シミュレーション
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
using UnityEngine; | |
using System.Collections.Generic; | |
using PlateauToolkit.Rendering; | |
public class CameraController : MonoBehaviour | |
{ | |
public float moveSpeed = 10f; // カメラの移動速度 | |
public float aerialHeightOffset = 20f; // 上空モードの高さオフセット | |
public Vector3 clickOffset = new Vector3(0, 0, 0); // クリック地点からのオフセット | |
public List<Transform> objectsToWatch; // 注視するオブジェクトのリスト | |
public float dayValue; | |
public float nightValue; | |
private Vector3 initialPosition; // カメラの初期位置 | |
private bool isGroundMode = false; // 地上モードかどうかのフラグ | |
private int currentObjectIndex = 0; // 現在注視しているオブジェクトのインデックス | |
EnvironmentController m_EnvironmentController; | |
void Start() | |
{ | |
// カメラの初期位置を設定し、真下を向かせる | |
initialPosition = transform.position; | |
initialPosition.y = aerialHeightOffset; | |
transform.position = initialPosition; | |
transform.rotation = Quaternion.Euler(90f, 0f, 0f); | |
m_EnvironmentController = FindObjectOfType<EnvironmentController>(); | |
} | |
void Update() | |
{ | |
if (!isGroundMode) | |
{ | |
if (Input.GetMouseButtonDown(0)) | |
{ | |
MoveToClickPosition(); | |
} | |
else | |
{ | |
MoveCamera(); | |
} | |
} | |
else | |
{ | |
WatchObject(); | |
if (Input.GetKeyDown(KeyCode.UpArrow)) | |
{ | |
ResetToAerialPosition(); | |
} | |
} | |
} | |
void MoveCamera() | |
{ | |
// 入力に応じてカメラを地面と平行に移動させる | |
float moveX = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime; | |
float moveZ = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime; | |
Vector3 move = new Vector3(moveX, 0, moveZ); | |
transform.Translate(move, Space.World); | |
// カメラの高さを常に固定する | |
Vector3 position = transform.position; | |
position.y = aerialHeightOffset; | |
transform.position = position; | |
} | |
void MoveToClickPosition() | |
{ | |
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); | |
RaycastHit hit; | |
if (Physics.Raycast(ray, out hit)) | |
{ | |
Vector3 targetPosition = hit.point + clickOffset; | |
transform.position = targetPosition; | |
m_EnvironmentController.m_TimeOfDay = nightValue; | |
isGroundMode = true; | |
LookAtCurrentObject(); | |
} | |
} | |
void ResetToAerialPosition() | |
{ | |
Vector3 position = transform.position; | |
position.y = aerialHeightOffset; | |
transform.position = position; | |
transform.rotation = Quaternion.Euler(90f, 0f, 0f); // 真下を向く | |
isGroundMode = false; | |
m_EnvironmentController.m_TimeOfDay = dayValue; | |
} | |
void WatchObject() | |
{ | |
if (Input.GetKeyDown(KeyCode.LeftArrow)) | |
{ | |
currentObjectIndex = (currentObjectIndex - 1 + objectsToWatch.Count) % objectsToWatch.Count; | |
LookAtCurrentObject(); | |
} | |
else if (Input.GetKeyDown(KeyCode.RightArrow)) | |
{ | |
currentObjectIndex = (currentObjectIndex + 1) % objectsToWatch.Count; | |
LookAtCurrentObject(); | |
} | |
} | |
void LookAtCurrentObject() | |
{ | |
if (objectsToWatch.Count > 0) | |
{ | |
transform.LookAt(objectsToWatch[currentObjectIndex]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment