前提・実現したいこと
こちらのサイトでRotateAroundメソッドの回転半径の設定について勉強しています。
円の中心から遠ざける場合のコードについて疑問点があるので、ご教示お願いします。
該当のソースコード
こちらのコードなのですが、
C#
1// カメラを挟んで、物体とは反対側の座標 2var otherSide = target.position - diff * 2;
下記のように書き換えて問題ありませんか?
C#
1var otherSide = this.transform.position - diff;
個人的には、this.transform.positionを起点にしてdiffの距離遠ざけるこの書き換えの方が
直感的でしっくりくるのですが、
元のコードは、target.positionを起点にして、diff * 2の距離遠ざけていますが、何か意図はありますか?
それとも、今回のコードに関しては、下記は同等と認識してよいですか?
C#
1var otherSide = target.position - diff * 2; 2var otherSide = this.transform.position - diff;
(ちなみにどちらのコードでも無限に遠ざかりますよね?)
試したこと
カメラだとわかりづらいので、円の中心を球、回転するオブジェクトをCubeにして、
下記スクリプトをCubeにアタッチして試してみました。
動き的には変わりなさそうに見えましたが、
今回の書き換えで不具合が生じる部分がないか質問させていただきました。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Sample : MonoBehaviour 6{ 7 [SerializeField] 8 GameObject target; 9 10 float zoomSpeed = 10f; // ズームの速度 11 float minimumDistance = 1f; // 対象物との最小距離(これ以上接近しない) 12 float rotateSpeed = 50f; // 回転の速度 13 14 // Start is called before the first frame update 15 void Start() 16 { 17 18 } 19 20 // Update is called once per frame 21 void Update () 22 { 23 // 対象物とカメラとの相対座標 24 var diff = target.transform.position - this.transform.position; 25 26 // Aキーで対象物に接近する(最小距離よりは接近しない) 27 if ( Input.GetKey ( KeyCode.A ) && diff.sqrMagnitude > Mathf.Pow ( minimumDistance, 2 ) ) 28 { 29 this.transform.position = Vector3.MoveTowards (this.transform.position, target.transform.position, zoomSpeed * Time.deltaTime ); 30 } 31 // Sキーで対象物から遠ざかる 32 else if ( Input.GetKey ( KeyCode.S ) ) 33 { 34 // カメラを挟んで、物体とは反対側の座標 35 // var otherSide = target.transform.position - diff * 2; 36 var otherSide = this.transform.position - diff; 37 38 transform.position = Vector3.MoveTowards ( transform.position, otherSide, zoomSpeed * Time.deltaTime ); 39 } 40 41 // 左右キーで対象物の周りを回転する 42 if ( Input.GetKey ( KeyCode.LeftArrow ) ) 43 { 44 transform.RotateAround ( target.transform.position, Vector3.up, rotateSpeed * Time.deltaTime ); 45 } 46 else if ( Input.GetKey ( KeyCode.RightArrow ) ) 47 { 48 transform.RotateAround ( target.transform.position, -Vector3.up, rotateSpeed * Time.deltaTime); 49 } 50 } 51}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/06/03 15:23 編集