-
-
Save todorok1/9883cca205358013ceab46c0a9ffb83e to your computer and use it in GitHub Desktop.
オブジェクトを円周上で移動させるスクリプトのサンプル
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 System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| /// <Summary> | |
| /// オブジェクトを円周上で移動させるスクリプトです。 | |
| /// </Summary> | |
| public class SphereMover : MonoBehaviour | |
| { | |
| // 円の半径を設定します。 | |
| public float radius = 10f; | |
| // 初期位置を取得し、高さを保持します。 | |
| Vector3 initPos; | |
| void Start() | |
| { | |
| // 初期位置を保持します。 | |
| initPos = gameObject.transform.position; | |
| } | |
| void Update() | |
| { | |
| CalcPosition(); | |
| } | |
| /// <Summary> | |
| /// オブジェクトの位置を計算するメソッドです。 | |
| /// </Summary> | |
| void CalcPosition() | |
| { | |
| // 位相を計算します。 | |
| float phase = Time.time * 2 * Mathf.PI; | |
| // 現在の位置を計算します。 | |
| float xPos = radius * Mathf.Cos(phase); | |
| float zPos = radius * Mathf.Sin(phase); | |
| // ゲームオブジェクトの位置を設定します。 | |
| Vector3 pos = new Vector3(xPos, initPos.y, zPos); | |
| gameObject.transform.position = pos; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment