以前の処理では途中で半径を変えられなかったため、半径を変える処理を追加したものが以下になります
using UnityEngine; public class RoundTarget : MonoBehaviour { [SerializeField] private Transform _center; [SerializeField] private float _radius = 1f; [SerializeField] private float _speed = 1f; private Vector3 _currentAngle; private Vector3 _nextPos; #if UNITY_EDITOR private void OnDrawGizmos() { var center = _center.transform.position; Gizmos.DrawWireSphere(center, _radius); } #endif private void Update() { if (Input.anyKey) { if (Input.GetKey(KeyCode.A)) { UpdatePosition(Vector3.left); } if (Input.GetKey(KeyCode.D)) { UpdatePosition(Vector3.right); } if (Input.GetKey(KeyCode.W)) { UpdatePosition(Vector3.up); } if (Input.GetKey(KeyCode.S)) { UpdatePosition(Vector3.down); } if (Input.GetKey(KeyCode.UpArrow)) { UpdatePosition(Vector3.forward); } if (Input.GetKey(KeyCode.DownArrow)) { UpdatePosition(Vector3.back); } return; } UpdatePosition(Vector3.zero); } private void UpdatePosition(Vector3 delta) { var angle = delta * _speed; for (var i = 0; i < 2; i++) { _currentAngle[i] = (_currentAngle[i] + angle[i]) % 360; } _radius += delta.z * _speed / 360f; // 高さとXの半径を調べてそれをもとにXZを計算する _nextPos.y = Mathf.Sin(_currentAngle.y * Mathf.Deg2Rad) * _radius; var radius = Mathf.Cos(_currentAngle.y * Mathf.Deg2Rad) * _radius; _nextPos.x = Mathf.Cos(_currentAngle.x * Mathf.Deg2Rad) * radius; _nextPos.z = Mathf.Sin(_currentAngle.x * Mathf.Deg2Rad) * radius; transform.position = _center.transform.position + _nextPos; } }