unityにて攻撃モーションによる座標変化をアニメーションと連動させたいためrootmotionを用いています。
しかし、特定の条件下で上手く動きません。
具体的にはコード中のFixedUpdateを消すと正常に動くので、そのなかのどれかが邪魔をしているのですが原因を特定することができません。
カメラの正面を移動時のプレイヤーの正面とする処理を記述しているのですが、どうやらそこが邪魔をしているらしいということはわかっています。
ご教授お願いします。
C#
1コードusing System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class playermove : MonoBehaviour 6{ 7 public float speed = 20.0f; 8 public float jumpspeed = 10.0f; 9 public Rigidbody rb; 10 public Animator am; 11 12 float inputHorizontal; 13 float inputVertical; 14 15 bool grounded ; 16 bool inputspace; 17 bool space; 18 19 void Start() 20 { 21 rb = GetComponent<Rigidbody>(); 22 am = GetComponent<Animator>(); 23 } 24 25 void Update() 26 { 27 int keystate = 0; 28 29 //handle animations 30 if (Input.GetKey("w")) 31 { 32 keystate += 1; 33 } 34 if (Input.GetKey("s")) 35 { 36 keystate += 2; 37 } 38 if (Input.GetKey("d")) 39 { 40 keystate += 4; 41 } 42 if (Input.GetKey("a")) 43 { 44 keystate += 8; 45 } 46 47 if (Input.GetKeyUp("w")) 48 { 49 keystate -= 1; 50 } 51 if (Input.GetKeyUp("s")) 52 { 53 keystate -= 2; 54 } 55 if (Input.GetKeyUp("d")) 56 { 57 keystate -= 4; 58 } 59 if (Input.GetKeyUp("a")) 60 { 61 keystate -= 8; 62 } 63 64 switch (keystate) 65 { 66 case 0: 67 am.SetBool("dash", false); 68 am.SetBool("escape", false); 69 break; 70 case 1://front 71 am.SetBool("dash", true); 72 break; 73 case 2://back 74 am.SetBool("dash", true); 75 break; 76 case 4://right 77 am.SetBool("dash", true); 78 break; 79 case 8://left 80 am.SetBool("dash", true); 81 break; 82 case 5://Rfront 83 am.SetBool("dash", true); 84 break; 85 case 9://Lfront 86 am.SetBool("dash", true); 87 break; 88 case 6://Rback 89 am.SetBool("dash", true); 90 break; 91 case 10://Lback 92 am.SetBool("dash", true); 93 break; 94 } 95 if (Input.GetKey("left shift")) 96 { 97 am.SetBool("escape", true); 98 } 99 else 100 { 101 am.SetBool("escape", false); 102 } 103 104 if (am.GetCurrentAnimatorStateInfo(0).IsName("attack") == false 105 && am.GetCurrentAnimatorStateInfo(0).IsName("attack2") == false 106 && am.GetCurrentAnimatorStateInfo(0).IsName("attack3") == false 107 && am.GetCurrentAnimatorStateInfo(0).IsName("combo1") == false) 108 { 109 inputHorizontal = Input.GetAxis("Horizontal"); 110 inputVertical = Input.GetAxis("Vertical"); 111 inputspace = Input.GetButton("Jump"); 112 } 113 114 } 115 116 void OnCollisionEnter(Collision other) 117 { 118 grounded = false; 119 space = false; 120 } 121 void OnCollisionStay(Collision other) 122 { 123 grounded = true; 124 } 125 126 void FixedUpdate() 127 { 128 //この下 129 // カメラの方向から、X-Z平面の単位ベクトルを取得 130 Vector3 cameraForward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized; 131 132 // 方向キーの入力値とカメラの向きから、移動方向を決定 133 Vector3 moveForward = cameraForward * inputVertical + Camera.main.transform.right * inputHorizontal; 134 135 // 移動方向にスピードを掛ける 136 rb.velocity = moveForward * speed + new Vector3(0, rb.velocity.y, 0); 137 138 //この上 139 if (am.GetCurrentAnimatorStateInfo(0).IsName("attack") == true 140 || am.GetCurrentAnimatorStateInfo(0).IsName("attack2") == true 141 || am.GetCurrentAnimatorStateInfo(0).IsName("attack3") == true 142 || am.GetCurrentAnimatorStateInfo(0).IsName("combo1") == true) 143 { 144 rb.velocity = moveForward * 0 + new Vector3(0, 0, 0); 145 } 146 147 // キャラクターの向きを進行方向に 148 if (moveForward != Vector3.zero) 149 { 150 transform.rotation = Quaternion.LookRotation(moveForward); 151 } 152 153 if (grounded) 154 { 155 if(space == false) 156 { 157 if (inputspace == true) 158 { 159 space = true; 160 am.SetBool("jump", true); 161 rb.velocity = new Vector3(0, jumpspeed, 0); 162 } 163 } 164 } 165 else 166 { 167 am.SetBool("jump", false); 168 } 169 } 170}
C#
1 void FixedUpdate() 2 { 3 //この下 4 // カメラの方向から、X-Z平面の単位ベクトルを取得 5 Vector3 cameraForward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized; 6 7 // 方向キーの入力値とカメラの向きから、移動方向を決定 8 Vector3 moveForward = cameraForward * inputVertical + Camera.main.transform.right * inputHorizontal; 9 10 // 移動方向にスピードを掛ける 11 rb.velocity = moveForward * speed + new Vector3(0, rb.velocity.y, 0); 12 13 //この上 14 if (am.GetCurrentAnimatorStateInfo(0).IsName("attack") == true 15 || am.GetCurrentAnimatorStateInfo(0).IsName("attack2") == true 16 || am.GetCurrentAnimatorStateInfo(0).IsName("attack3") == true 17 || am.GetCurrentAnimatorStateInfo(0).IsName("combo1") == true) 18 { 19 rb.velocity = moveForward * 0 + new Vector3(0, 0, 0); 20 } 21 22 // キャラクターの向きを進行方向に 23 if (moveForward != Vector3.zero) 24 { 25 transform.rotation = Quaternion.LookRotation(moveForward); 26 } 27 28 if (grounded) 29 { 30 if(space == false) 31 { 32 if (inputspace == true) 33 { 34 space = true; 35 am.SetBool("jump", true); 36 rb.velocity = new Vector3(0, jumpspeed, 0); 37 } 38 } 39 } 40 else 41 { 42 am.SetBool("jump", false); 43 } 44 }v
エラー内容は書きましょう
「うまく動きません」ではなく、想定の動作と実際の動作を書かないとわかりません
あなたの回答
tips
プレビュー