#実現したいこと
カメラのズームと移動を切り離して実行したい。
#問題点
現在、GearVRのコントローラーのタッチパッドからの入力を使用して、カメラが物体の回りを球体座標内で回転・ズームすることができる、カメラの動きに関するスクリプトを書いています。
回転:タッチパッドをクリックしている間、4分割した方向に合わせて回転
ズーム:トリガー+上クリックでズームイン、トリガー+下クリックでズームアウト
※タッチパッドは上・下・右・左に相当するように4分割にしています
ズームインをしようとトリガーと上クリックしても、回転処理が優先され、回転の動きが止まってから(y軸方向はClampで制限)ズームの処理が始まってしまいます。
そのため、カメラが物体の正面方向を向いている状態でズームインをしても、物体の上方向を向いてからのズームとなり、意図したズームの動きとは異なってしまいます。
#試したこと
コルーチンを用いて、回転とズームを切り離して処理を行うようにすれば解決できるのではないかとスクリプトを書いてみましたが、一向に改善されません。
該当スクリプトは以下の通りです。
プログラミング初心者になるので見にくいかもしれませんがご容赦ください。
解決方法だけでなく、現在のスクリプトでもっとこうしたら良くなるとかもありましたらご教示いただければ幸いです。
よろしくお願い致します。
C#
1public class CoroCam : MonoBehaviour 2{ 3 4 public Transform target; 5 public float spinSpeed = 1f; 6 public float distance = 1.2f; 7 public float diffR = 0.1f; 8 9 Vector3 nowPos; 10 Vector3 pos = Vector3.zero; 11 public Vector2 pad = Vector2.zero; 12 13 Transform currentTarget; 14 Vector3 currentTargetCenter; 15 Vector2 touchPosition; 16 17 bool isZooming = (OVRInput.Get(OVRInput.Button.PrimaryIndexTrigger)); 18 19 20 void Start() 21 { 22 // 初期位置の取得 23 nowPos = transform.position; 24 } 25 26 27 void Update() 28 { 29 StartCoroutine(CamZoom()); 30 StartCoroutine(CamRoatation()); 31 32 UpdateCurrentTarget(); 33 transform.position = pos + currentTargetCenter; 34 transform.LookAt(currentTargetCenter); 35 } 36 37 38 39 40 IEnumerator CamRoatation() 41 { 42 if(isZooming) 43 yield break; 44 45 46 if (!isZooming) 47 { 48 if (OVRInput.Get(OVRInput.Button.PrimaryTouchpad)) 49 { 50 if (OVRInput.Get(OVRInput.Touch.PrimaryTouchpad)) 51 { 52 touchPosition = OVRInput.Get(OVRInput.Axis2D.PrimaryTouchpad); 53 54 55 //極座標系内での移動 56 if (touchPosition.y / touchPosition.x > 1 || touchPosition.y / touchPosition.x < -1) 57 { 58 if (touchPosition.y > 0) 59 { 60 pad.y += spinSpeed / 60; 61 } 62 else 63 { 64 pad.y -= spinSpeed / 60; 65 } 66 } 67 else 68 { 69 if (touchPosition.x > 0) 70 { 71 pad.x += spinSpeed / 60; 72 } 73 else 74 { 75 pad.x -= spinSpeed / 60; 76 } 77 } 78 79 } 80 } 81 82 } 83 84 85 86 pad.y = Mathf.Clamp(pad.y, 0.1f, 0.9f); 87 88 pos.x = distance * Mathf.Sin(pad.y * Mathf.PI) * Mathf.Cos(pad.x * Mathf.PI); 89 pos.y = distance * Mathf.Cos(pad.y * Mathf.PI); 90 pos.z = -distance * Mathf.Sin(pad.y * Mathf.PI) * Mathf.Sin(pad.x * Mathf.PI); 91 92 pos *= nowPos.z; 93 pos.y += nowPos.y; 94 95 yield return null; 96 } 97 98 99 100 IEnumerator CamZoom() 101 { 102 if (OVRInput.Get(OVRInput.Button.PrimaryTouchpad)) 103 { 104 if (OVRInput.Get(OVRInput.Touch.PrimaryTouchpad)) 105 { 106 touchPosition = OVRInput.Get(OVRInput.Axis2D.PrimaryTouchpad); 107 108 if ((OVRInput.Get(OVRInput.Button.PrimaryIndexTrigger) && ((touchPosition.y / touchPosition.x > 1 || touchPosition.y / touchPosition.x < -1)))) 109 { 110 if (touchPosition.y < 0) 111 { 112 distance += diffR * Time.deltaTime; 113 } 114 if (touchPosition.y > 0) 115 { 116 distance -= diffR * Time.deltaTime; 117 } 118 } 119 } 120 } 121 distance = Mathf.Max(distance, 0.1f); 122 123 yield return null; 124 } 125
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/19 14:26
2018/04/19 14:46 編集
2018/04/19 15:07 編集
2018/04/19 15:37
2018/04/20 03:36
2018/04/20 09:32
2018/04/21 08:04