-
-
Save yoh7686/d5eb394b14ce0bbf3593e9b7b024723a to your computer and use it in GitHub Desktop.
Roll a Ball Unity 6
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; | |
| public class PickUp : MonoBehaviour | |
| { | |
| void Update() | |
| { | |
| // オブジェクトを継続的に回転させる | |
| // X軸around 15度/秒、Y軸around 30度/秒、Z軸around 45度/秒の速度で回転 | |
| transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime); | |
| } | |
| void OnTriggerEnter(Collider other) | |
| { | |
| // トリガーに入ったオブジェクトがプレイヤーかどうかを確認 | |
| if (other.gameObject.CompareTag("Player")) | |
| { | |
| // プレイヤーだった場合、このPickupオブジェクトを破棄(収集) | |
| Destroy(gameObject); | |
| // ここに、スコア加算やサウンド再生などの追加処理を入れることができます | |
| } | |
| } | |
| } |
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 UnityEngine.InputSystem; | |
| public class PlayerController : MonoBehaviour | |
| { | |
| // プレイヤーの移動速度 | |
| public float speed = 10f; | |
| // Rigidbodyコンポーネントへの参照 | |
| private Rigidbody rb; | |
| // 入力された移動ベクトル | |
| private Vector2 moveInput = Vector2.zero; | |
| void Start() | |
| { | |
| // Rigidbodyコンポーネントを取得 | |
| rb = GetComponent<Rigidbody>(); | |
| } | |
| // Input Systemからの移動入力を処理するメソッド | |
| void OnMove(InputValue movementValue) | |
| { | |
| // 入力値を2Dベクトルとして取得 | |
| moveInput = movementValue.Get<Vector2>(); | |
| } | |
| void FixedUpdate() | |
| { | |
| // 入力ベクトルを3D空間の移動ベクトルに変換 | |
| // Y軸(垂直方向)の移動は0に設定 | |
| Vector3 movement = new Vector3(moveInput.x, 0.0f, moveInput.y); | |
| // Rigidbodyに力を加えてプレイヤーを移動 | |
| rb.AddForce(movement * speed); | |
| } | |
| } |
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 TMPro; | |
| public class ScoreManager : MonoBehaviour | |
| { | |
| // スコアを表示するTextMeshPro | |
| private TextMeshProUGUI scoreText; | |
| // ゲームクリアを表示するTextMeshPro | |
| public TextMeshProUGUI gameClearText; | |
| // 初期のアイテム数 | |
| private int initialItemCount; | |
| // 現在のアイテム数 | |
| private int currentItemCount; | |
| // スコア | |
| private int score = 0; | |
| // 経過時間 | |
| private float elapsedTime = 0f; | |
| // ゲームが終了したかどうか | |
| private bool isGameFinished = false; | |
| void Start() | |
| { | |
| // スコアを表示するTextMeshProコンポーネントを取得 | |
| scoreText = GetComponent<TextMeshProUGUI>(); | |
| // 'Item'タグの付いたオブジェクトの初期数を数える | |
| initialItemCount = GameObject.FindGameObjectsWithTag("Item").Length; | |
| currentItemCount = initialItemCount; | |
| // ゲームクリアテキストを非表示にする | |
| gameClearText.gameObject.SetActive(false); | |
| // 初期スコアを表示 | |
| UpdateScoreDisplay(); | |
| } | |
| void Update() | |
| { | |
| if (!isGameFinished) | |
| { | |
| // 経過時間を更新 | |
| elapsedTime += Time.deltaTime; | |
| // 現在のアイテム数を数える | |
| int newItemCount = GameObject.FindGameObjectsWithTag("Item").Length; | |
| // アイテムが減っていたらスコアを増やす | |
| if (newItemCount < currentItemCount) | |
| { | |
| score += currentItemCount - newItemCount; | |
| currentItemCount = newItemCount; | |
| } | |
| // スコア表示を更新 | |
| UpdateScoreDisplay(); | |
| // すべてのアイテムが回収されたらゲーム終了 | |
| if (currentItemCount == 0) | |
| { | |
| isGameFinished = true; | |
| gameClearText.gameObject.SetActive(true); | |
| } | |
| } | |
| } | |
| void UpdateScoreDisplay() | |
| { | |
| // スコアと時間を別々の行に表示 | |
| scoreText.text = $"Score: {score}\nTime: {elapsedTime:000.00}"; | |
| } | |
| } |
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; | |
| public class SimpleCameraFollow : MonoBehaviour | |
| { | |
| // 追跡する対象(Playerオブジェクト) | |
| public Transform target; | |
| // カメラとターゲットの初期相対位置 | |
| private Vector3 offset; | |
| void Start() | |
| { | |
| // ゲーム開始時のカメラとターゲットの相対位置を計算し、保存 | |
| offset = transform.position - target.position; | |
| } | |
| void LateUpdate() | |
| { | |
| // ターゲットの現在位置に、初期オフセットを加えてカメラの位置を更新 | |
| transform.position = target.position + offset; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment