###現状と要望
画面をDragをした際にDragの移動量でカメラを動かすスクリプトでカメラを制御しています。
(uGUIのPanelにImageをアタッチし、画像を透過して入力を検知しています)
縦方向に制限を設けるべくMathf.Clampで回転に対する入力値に制限を設け、一定以上の角度には
カメラが向かないようにしたのですが、こうするとカメラが制限値に達した後、下方向にドラッグすると、
一瞬で下の制限値にカメラが向いてしまいます。
なぜこうなってしまうのかを探ってみたのですが、結局自力では解明できなかったので、
カメラが制限値の方に向いてしまう理由と回避策が知りたいです。
カメラの制御スクリプト
cs
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.EventSystems; 5using UnityEngine.UI; 6public class CamContorl : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerDownHandler, IPointerUpHandler 7{ 8 public GameObject MainCamera; 9 public GameObject TargetObject; 10 11 private Vector3 lastMousePosition; 12 private Vector2 lastTouchPosition; 13 private Vector3 newAngle = new Vector3(0, 0, 0); 14 15 int touchID = -10; 16 bool a = true; 17 public void Start() 18 {} 19 20 public void OnBeginDrag(PointerEventData eventData) 21 { 22 if(touchID == -10) 23 { 24 touchID = eventData.pointerId; 25 } 26 } 27 28 public void OnEndDrag(PointerEventData eventData) 29 { 30 if(touchID == eventData.pointerId) 31 { 32 touchID = -10; 33 } 34 } 35 36 public void OnPointerDown(PointerEventData eventData) 37 { 38 newAngle = MainCamera.transform.localEulerAngles; 39 lastMousePosition = Input.mousePosition; 40 } 41 42 public void OnPointerUp(PointerEventData eventData) 43 { 44 lastMousePosition = Input.mousePosition; 45 } 46 47 public void OnDrag(PointerEventData eventData) 48 { 49 if(lastMousePosition.y >= Screen.height * 0.28f || lastMousePosition.x >= Screen.width * 0.25f) 50 { 51 newAngle.y += (Input.mousePosition.x - lastMousePosition.x) * 0.015f; 52 newAngle.x -= (Input.mousePosition.y - lastMousePosition.y) * 0.015f; 53 54 55 //newAngle.x = Mathf.Clamp(newAngle.x, -90, 90); 56 if(newAngle.x > 90) 57 { 58 if(Input.mousePosition.y != 0 || Input.GetMouseButton(0)) 59 { 60 newAngle.x = 90; 61 MainCamera.transform.localEulerAngles = newAngle; 62 Debug.Log("!"); 63 } 64 }else if(newAngle.x < -90) 65 { 66 if(Input.mousePosition.y != 0 || Input.GetMouseButton(0)) 67 { 68 newAngle.x = -90; 69 MainCamera.transform.localEulerAngles = newAngle; 70 Debug.Log("?"); 71 } 72 }else 73 { 74 MainCamera.transform.localEulerAngles = newAngle; 75 Debug.Log("-"); 76 } 77 } 78 } 79}
試したこと
原因を調べるためにMathf.Clampと同じ処理をifで書き、合間にDebug.Logを仕込んで
カメラが下を向くタイミングでどの処理が発生しているかを調べました。
結果としては、一度カメラが-90、90を超えた後、-89~89の時に操作すると
中間の処理(-)を飛ばして下限の処理(!)が発生している、という事しかわかりませんでした...
補足情報(FW/ツールのバージョンなど)
Unity2019.1.4f1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。