実現したいこと
キャラクターの向きを移動方向に向けて移動したい。
前提
プログラムはUnityで初めて触って、勉強し始めて2か月です。
2Dのゲームをいくつか本を見ながら作成して、3Dのアクションゲームの作成にチャレンジしています。
アクションゲームも作り方のサイトを見ながら作成していますが、使用素材が違うため(使用素材の無料期間が終了している等)
サイト通りにうまくいかず苦戦している日々です。
キャラクターをなんとか移動させることまでは出来たのですが、キャラクターの向きを進行方向に向けようと
以下のコードを入力すると
一瞬だけ向きを変えてるような挙動はするのですが、すぐに向きが正面を向いた位置に戻ってしまいます。
bool isKeyInput = (horizontalKeyInput != 0 || verticalKeyInput != 0); if (isKeyInput == true) { Vector3 dir = rigid.velocity.normalized; dir.y = 0; this.transform.forward = dir; }
発生している問題・エラーメッセージ
Look rotation viewing vector is zero
UnityEngine.Transform:set_forward (UnityEngine.Vector3)
PlayerController3:Update () (at Assets/AppMain/PlayerController3.cs:41)
該当のソースコード
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController3 : MonoBehaviour { [SerializeField] GameObject attackHit = null; [SerializeField] ColliderCallReceive footColliderCall = null; [SerializeField] float jumpPower = 20f; Animator animator = null; Rigidbody rigid = null; //攻撃アニメーション中フラグ bool isAttack = false; //接地フラグ bool isGround = false; float horizontalKeyInput = 0; float verticalKeyInput = 0; // Start is called before the first frame update void Start() { animator = GetComponent<Animator>(); rigid = GetComponent<Rigidbody>(); attackHit.SetActive(false); footColliderCall.TriggerStayEvent.AddListener(OnFootTriggerStay); footColliderCall.TriggerExitEvent.AddListener(OnFootTriggerExit); } // Update is called once per frame void Update() { horizontalKeyInput = Input.GetAxis("Horizontal"); verticalKeyInput = Input.GetAxis("Vertical"); bool isKeyInput = (horizontalKeyInput != 0 || verticalKeyInput != 0); if (isKeyInput == true) { Vector3 dir = rigid.velocity.normalized; dir.y = 0; this.transform.forward = dir; } } void FixedUpdate() { Vector3 input = new Vector3(horizontalKeyInput, 0, verticalKeyInput); Vector3 move = input.normalized * 2f; Vector3 cameraMove = Camera.main.gameObject.transform.rotation * move; cameraMove.y = 0; rigid.AddForce(cameraMove - rigid.velocity, ForceMode.VelocityChange); } public void OnAttackButtonClicked() { if (isAttack == false) { animator.SetTrigger("isAttack"); isAttack = true; } } public void OnJumpButtonClicked() { if (isGround == true ) { rigid.AddForce(Vector3.up * jumpPower, ForceMode.Impulse); } } void OnFootTriggerStay( Collider col ) { if(col.gameObject.tag == "Ground" ) { if (isGround == false) isGround = true; if (animator.GetBool("isGround") == false) animator.SetBool("isGround", true); } } void OnFootTriggerExit( Collider col) { if (col.gameObject.tag == "Ground") { isGround = false; animator.SetBool("isGround", false); } } void Anim_AttackHit() { Debug.Log("Hit"); attackHit.SetActive(true); } void Anim_AttackEnd() { Debug.Log("End"); attackHit.SetActive(false); isAttack = false; } }
試したこと
同サイトの「進行方向に向き続けるように」の質問と回答を見ましたが
(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical")); で既にコードが違うことと
Vector3にzeroを入力していないので該当しないと思ったことと
同サイトの「[Unity] rb.velocity.normalized がゼロにならない。原因が知りたい。」の質問と回答を見ましたが
テレインに問題があるのだろうか?(使用素材のテレインが参考にしている作り方のサイトと違う)
とも考えましたが、よく分かりませんでした。
一応他のサイトでも調べて、よく分かっていないのですが
void start()部分に transform.rotation = Quaternion.identity; を入力してみましたが改善されませんでした。
更に他のサイトでも同様のエラーを調べましたがよくわかりませんでした。
再生した時にinspectorのTransformのRotationのY軸がすぐに0に戻るのが原因なのか
玉にですけど、進行方向にボタンを押す(離す?)タイミングによってはY軸に何かしらの数字が入った状態で止まるのですけど
その時は進行方向に向いた状態で止まるので。Y軸が何か問題になってるのかな?と勝手に思っています。
よく分かってない中、申し訳ありませんが分かりやすく解決策を教えて頂けると助かります。
補足情報(FW/ツールのバージョンなど)
Unityのバージョン 3.20f1
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。