前提・実現したいこと
UnityでFPSカメラを実装したい。
発生している問題・エラーメッセージ
FPSゲームのカメラを実装している最中に発生した問題。
rotationのZが負の数になった瞬間に正の数のリミットの値まで瞬間的に視点移動してしまう。
該当のソースコード
C#
1//LookController.cs 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5 6public class LookController : MonoBehaviour 7{ 8 9 [SerializeField] 10 private float LookLimitY = 80f; 11 12 private float limitInput = 60f; 13 14 void Start() 15 { 16 transform.Rotate(-transform.localEulerAngles); 17 Debug.Log(-5f > LookLimitY); 18 } 19 20 // Update is called once per frame 21 void Update() 22 { 23 24 } 25 26 private bool checkLook(float value){ 27 return (limitInput < value || -limitInput > value); 28 } 29 30 public void Look(float x,float y){ 31 32 if(checkLook(x) || checkLook(y)){ 33 return; 34 } 35 36 transform.Rotate(x,0f,y); 37 38 var angle = transform.localRotation.eulerAngles; 39 40 //多分ここらへんが原因 41 if(angle.z > LookLimitY){ 42 transform.localRotation = Quaternion.Euler(angle.x,angle.y,LookLimitY); 43 }else if(angle.z < -LookLimitY){ 44 transform.localRotation = Quaternion.Euler(angle.x,angle.y,-LookLimitY); 45 } 46 47 if(transform.localEulerAngles.z < 0){ 48 transform.position = new Vector3(0.5f/LookLimitY,transform.position.y,transform.position.z); 49 } 50 } 51}
C#
1//PlayerController.cs 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5using UnityEngine.InputSystem; 6 7public class PlayerContoroller : MonoBehaviour 8{ 9 [Header("playerにアタッチするhairのプレハブに設定されたAssetAddress")] 10 [SerializeField] 11 private string hairAddress; 12 13 [Header("playerのベースの移動スピード")] 14 [SerializeField] 15 private float baseSpeed = 10; 16 17 private float speed; 18 private float speedBoost; 19 20 private Mouse mouse; 21 private Vector2 mouseZero; 22 23 private Rigidbody _rigidbody; 24 25 private HeadAttacher headAttacher; 26 private LookController lookController; 27 28 private float moveX; 29 private float moveZ; 30 31 private float lookX; 32 private float lookY; 33 34 private bool warpCursorPosition; 35 36 const float speedCorrection = 100; 37 38 public void onMove(InputAction.CallbackContext context){ 39 var vector2 = context.ReadValue<Vector2>(); 40 moveX = vector2.x; 41 moveZ = vector2.y; 42 } 43 44 public void onLook(InputAction.CallbackContext context){ 45 var vector2 = context.ReadValue<Vector2>(); 46 lookX = vector2.x; 47 lookY = vector2.y; 48 } 49 50 51 void Start() 52 { 53 // warpCursorPosition = false; 54 55 // Cursor.lockState = CursorLockMode.Confined; 56 57 speedBoost = 1; 58 _rigidbody = GetComponent<Rigidbody>(); 59 60 headAttacher = transform.Find("neck").Find("head attacher").gameObject.GetComponent<HeadAttacher>(); 61 headAttacher.HairPrefabAddress = hairAddress; 62 63 mouse = Mouse.current; 64 mouseZero = new Vector2(0f,0f); 65 lookController = transform.Find("neck").gameObject.GetComponent<LookController>(); 66 } 67 68 void FixedUpdate(){ 69 _rigidbody.MovePosition(_rigidbody.position + new Vector3(moveZ*speed,0f,moveX*speed)); 70 } 71 72 // Update is called once per frame 73 void Update() 74 { 75 76 speed = baseSpeed/speedCorrection*speedBoost; 77 78 79 lookController.Look(0f,lookY); 80 } 81} 82
試したこと
最初はlocalEulerAnglesを使っていて、それがだめなのかと思い、QuaternionとlocalRotationを使うように変えたが失敗
視点のリミット機能をコメントアウトするとこの問題は起きない。
補足情報(FW/ツールのバージョンなど)
unity 2020.3.0f1
InputSystem 1.0.2
あなたの回答
tips
プレビュー