Unity3Dで分からないことがあります
https://unity-shoshinsha.biz/archives/574
こちらのサイトと
https://tech.pjin.jp/blog/2016/11/04/unity_skill_5/
こちらのサイトを参考にカメラとプレイヤーの動きをスクリプトで作っているのですが
とある条件のもとでカメラがブレます
どんな条件かと言うと「メインカメラの親オブジェクトのTransform.Rotaion.xの値が90°或いは-90°になるとブレる」ということです
Transform.Rotation.yは正常に動きます
文字で書いてもよく分からないと思うのでTwitterに動画をあげてあるのでそちらでご確認ください
https://twitter.com/0191otukiApp/status/1111610522493816833
コードを載せます
上記に挙げたサイトを自分なりに改変してみたものです
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class CameraController : MonoBehaviour 6{ 7 8 //MainCameraが回転するスピード 9 [SerializeField] 10 float rotate_speed = 3; 11 12 //CameraControllerの座標を指定する変数 13 //CameraControllerはMain Cameraの親オブジェクト 14 [SerializeField] 15 Vector3 CameraControllerPos; 16 17 GameObject Player; 18 GameObject MainCamera; 19 20 // Start is called before the first frame update 21 void Start() 22 { 23 Player = GameObject.Find("Player"); 24 MainCamera = GameObject.Find("Main Camera"); 25 26 //Cameraの初期位置を設定 27 MainCamera.transform.position = new Vector3(0, 0, -3); 28 //CameraとCameraControllerの角度を最初だけそろえる 29 MainCamera.transform.localRotation = transform.rotation; 30 } 31 32 // Update is called once per frame 33 void LateUpdate() 34 { 35 //CameraControllerの位置をPlayerの位置+CameraControllerPosで決める 36 transform.position = Player.transform.position + CameraControllerPos; 37 38 //Cameraの角度にマウスから取った角度を入れる 39 transform.eulerAngles += new Vector3(Input.GetAxis("Mouse Y") * rotate_speed * -1, Input.GetAxis("Mouse X") * rotate_speed, 0); 40 } 41}
一応プレイヤー側のコードも載せておきます
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class PlayerController : MonoBehaviour { 6 7 float InputHorizontal; 8 float InputVertical; 9 10 float MoveSpeed = 3.0f; 11 12 //Animatorを入れる 13 Animator PlayerAnimator; 14 15 //Rigidbodyを入れる 16 Rigidbody PlayerRigit; 17 18 //Capsule Colliderを入れる 19 CapsuleCollider PlayerCollider; 20 21 // Start is called before the first frame update 22 void Start() 23 { 24 PlayerAnimator = GetComponent<Animator>(); 25 PlayerRigit = GetComponent<Rigidbody>(); 26 PlayerCollider = GetComponent<CapsuleCollider>(); 27 } 28 29 // Update is called once per frame 30 void Update() 31 { 32 33 InputHorizontal = Input.GetAxisRaw("Horizontal"); 34 InputVertical = Input.GetAxisRaw("Vertical"); 35 36 if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.D)) 37 { 38 PlayerAnimator.SetTrigger("Run"); 39 } 40 else if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.D)) 41 { 42 PlayerAnimator.SetTrigger("Idle"); 43 } 44 } 45 46 void FixedUpdate() 47 { 48 // カメラの方向から、X-Z平面の単位ベクトルを取得 49 Vector3 cameraForward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized; 50 51 // 方向キーの入力値とカメラの向きから、移動方向を決定 52 Vector3 moveForward = cameraForward * InputVertical + Camera.main.transform.right * InputHorizontal; 53 54 // 移動方向にスピードを掛ける。ジャンプや落下がある場合は、別途Y軸方向の速度ベクトルを足す。 55 PlayerRigit.velocity = moveForward * MoveSpeed + new Vector3(0, PlayerRigit.velocity.y, 0); 56 57 // キャラクターの向きを進行方向に 58 if (moveForward != Vector3.zero) 59 { 60 transform.rotation = Quaternion.LookRotation(moveForward); 61 } 62 } 63}
どなたか教えて下さいお願いします
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/03/30 08:29
退会済みユーザー
2019/03/30 08:47
退会済みユーザー
2019/03/30 08:49
退会済みユーザー
2019/03/30 12:54
退会済みユーザー
2019/03/31 09:03