tpsのゲームのカメラ制御を作っている際に、このようなプログラムを書いていたら
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class PlayerCam : MonoBehaviour 6{ 7 public GameObject targetObj; 8 public float camRotateHorizontalSpeed; 9 public float camRotateVerticalSpeed; 10 public float camJoyStickRotateHorizontalSpeed; 11 public float camJoyStickRotateVerticntalSpeed; 12 13 public float minVerticalLimit; 14 public float maxVerticalLimit; 15 16 public Vector3 defaulltCamPos; 17 bool afterSetFirstFrame; 18 Vector3 prevTargetPos; 19 20 public bool cursorLocking; 21 22 [SerializeField] GameObject pauseUI; 23 24 // Start is called before the first frame update 25 void Start() 26 { 27 targetObj = null; 28 afterSetFirstFrame = true; 29 //カーソルをロック 30 cursorLocking = true; 31 Cursor.lockState = CursorLockMode.Locked; 32 Cursor.visible = false; 33 pauseUI.SetActive(false); 34 } 35 36 // Update is called once per frame 37 void Update() 38 { 39 if (Input.GetKeyDown(KeyCode.Escape)) 40 { 41 //カーソルロック解除 42 Cursor.lockState = CursorLockMode.None; 43 Cursor.visible = true; 44 cursorLocking = false; 45 pauseUI.SetActive(true); 46 } 47 48 if (targetObj != null) 49 { 50 if (afterSetFirstFrame) 51 { 52 afterSetFirstFrame = false; 53 prevTargetPos = targetObj.transform.position; 54 transform.position = prevTargetPos + defaulltCamPos; 55 } 56 else 57 { 58 //↓ProgressOfCam↓ 59 //move 60 transform.position += targetObj.transform.position - prevTargetPos; 61 prevTargetPos = targetObj.transform.position; 62 63 float mouseMoveX = Input.GetAxis("Mouse X"); 64 float mouseMoveY = Input.GetAxis("Mouse Y"); 65 float rightStickX = Input.GetAxis("RightStickHorizontal"); 66 float rightStickY = Input.GetAxis("RightStickVertical"); 67 68 if (cursorLocking) 69 { 70 //カメラの回転 71 transform.RotateAround(prevTargetPos, Vector3.up, (mouseMoveX + Input.GetAxis("RightStickHorizontal") * camJoyStickRotateHorizontalSpeed) * Time.deltaTime * camRotateHorizontalSpeed); 72 transform.RotateAround(prevTargetPos, transform.right, (mouseMoveY + Input.GetAxis("RightStickVertical") * camJoyStickRotateVerticntalSpeed) * Time.deltaTime * camRotateVerticalSpeed); 73 float fLEAX = fixedLocalEulerAngleX(); 74 75 Debug.Log("MouseX:" + mouseMoveX.ToString() + "MouseY:" + mouseMoveY.ToString() + "StickX:" + rightStickX.ToString() + "StickY:" + rightStickY.ToString() + "RotateX:" + transform.eulerAngles.x.ToString() + "FixedRotateX:" + fLEAX); 76 //!!!!問題点!!!!上限・下限を超えた場合にもとに戻す処理がうまくいかない 77 if (fLEAX < minVerticalLimit || fLEAX > maxVerticalLimit) 78 transform.RotateAround(prevTargetPos, transform.right, 0 - (mouseMoveY + Input.GetAxis("RightStickVertical") * camJoyStickRotateVerticntalSpeed) * Time.deltaTime * camRotateVerticalSpeed); 79 } 80 } 81 } 82 83 84 } 85 86 //localEulerAngle.xを-90<x<=270に修正 87 float fixedLocalEulerAngleX() 88 { 89 float lEAX = transform.localEulerAngles.x; 90 91 if (lEAX <= 270) return lEAX; else return lEAX - 360; 92 } 93} 94
マウスを縦に動かす大きさが大きすぎるとx軸の回転が大きくなりすぎて上下逆になってしまいます。
上限・下限を超した場合の計算がうまくいかないようです。
360°でx軸の角度を求められないのでしょうか?
または反転しないように他の工夫が出来るのでしょうか?
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/24 08:22