質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

2939閲覧

オブジェクトに当たった時にアニメーションさせたい

.com

総合スコア8

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2021/04/15 16:23

編集2021/04/17 14:13

前提・実現したいこと

Unityでオブジェクトに当たった時にアニメーションをさせたいです。
現状、OnCollisionEnter()で当たったという判定を取って、アニメーションをさせようとしています。ですが、遷移してくれません。

発生している問題・エラーメッセージ

判定自体はDebug.Logで出来ていることは確認したのですが、アニメーションをしてくれません。(floatやtriggerに1.0やチェックも入ることはありませんでした)

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class PlayerController : MonoBehaviour 6{ 7 // Rigidbodyを変数に入れる 8 Rigidbody rb; 9 // 移動スピード 10 private float f_Speed = 15.0f; 11 // ジャンプ力 12 public float f_JumpForce = 400.0f; 13 // Animatorを入れる変数 14 private Animator animator; 15 // プレイヤーの位置を入れる 16 Vector3 v_PlayerPos; 17 // プレイヤーの回転を入れる 18 Vector3 v_Rotation; 19 // 地面に接触しているか否か 20 bool b_Ground = true; 21 int n_Key = 0; 22 23 string state; 24 string prevState; 25 26 bool isStopTime = false; //時間停止 27 int n_JumpDirection = 0; //ジャンプした時の進行方向 bool値の様なもの 28 29 float hori; //Input.GetAxis("Horizontal") 30 31 void Start() 32 { 33 // Rigidbodyを取得 34 rb = GetComponent<Rigidbody>(); 35 // プレイヤーのAnimatorにアクセスする 36 animator = GetComponent<Animator>(); 37 // プレイヤーの現在より少し前の位置を保存 38 v_PlayerPos = transform.position; 39 40 transform.rotation = Quaternion.Euler(0, 90, 0); 41 } 42 43 void Update() 44 { 45 if (isStopTime == false) 46 { 47 rb.isKinematic = false; //重力を付ける 48 animator.speed = 1; //アニメーションの再開 49 50 ChangeAnimation(); 51 GetInputKey(); 52 ChangeState(); 53 Move(); 54 } 55 if (Input.GetKeyDown("joystick button 1")) 56 { 57 isStopTime = true; 58 StartCoroutine("StageRotTimeStop"); //コルーチン 59 } 60 } 61 62 IEnumerator StageRotTimeStop() //ステージを回転させているときのプレイヤーの時間を止める 63 { 64 rb.isKinematic = true; //重力を止める 65 animator.speed = 0; //アニメーションの一時停止 66 67 yield return new WaitForSeconds(0.45f); 68 69 GetInputKey(); 70 ChangeState(); 71 ChangeAnimation(); 72 Move(); 73 74 isStopTime = false; 75 } 76 77 void GetInputKey() 78 { 79 hori = Input.GetAxis("Horizontal"); 80 if (hori < 0) //左 81 { 82 n_Key = -1; 83 state = "RUN"; 84 85 //directionのX軸とZ軸の方向を向かせる 86 transform.rotation = Quaternion.LookRotation(new Vector3(-90, 0, 0)); 87 } 88 if (hori > 0) //右 89 { 90 n_Key = 1; 91 state = "RUN"; 92 93 //directionのX軸とZ軸の方向を向かせる 94 transform.rotation = Quaternion.LookRotation(new Vector3(90, 0, 0)); 95 } 96 else 97 { 98 state = "IDLE"; 99 } 100 } 101 102 // プレイヤーの状態を変える 103 void ChangeState() 104 { 105 if (b_Ground) 106 { 107 if (n_Key != 0) 108 { 109 state = "RUN"; 110 } 111 else 112 { 113 state = "IDLE"; 114 } 115 } 116 else 117 { 118 state = "JUMP"; 119 } 120 } 121 122 // プレイヤーのアニメーションを変える 123 void ChangeAnimation() 124 { 125 if (prevState != state) 126 { 127 switch (state) 128 { 129 case "JUMP": 130 animator.SetTrigger("Jump"); 131 break; 132 case "RUN": 133 animator.SetFloat("Run", 1.0f); 134 break; 135 case "PUSH": 136 animator.SetFloat("Push", 1.0f); 137 break; 138 case "CLEAR": 139 animator.SetTrigger("Clear"); 140 //animator.SetFloat("Clear", 1.0f); 141 break; 142 default: 143 break; 144 } 145 prevState = state; 146 } 147 } 148 149 // 動き 150 void Move() 151 { 152 if (b_Ground) 153 { 154 if (Input.GetKeyDown("joystick button 0")) 155 { 156 //jumpForceの分だけ上方に力がかかる 157 rb.AddForce(transform.up * f_JumpForce); 158 state = "JUMP"; 159 b_Ground = false; 160 161 if (n_Key < 0) 162 { 163 n_JumpDirection = -1; 164 } 165 if (n_Key > 0) 166 { 167 n_JumpDirection = 1; 168 } 169 170 if ((int)hori == 0) //垂直跳び時 171 { 172 n_JumpDirection = 0; 173 } 174 } 175 } 176 177 if (b_Ground == true) 178 { 179 // 現在の位置+入力した数値の場所に移動する 180 rb.MovePosition(transform.position + new Vector3(Input.GetAxisRaw("Horizontal") * Time.deltaTime * f_Speed, 0, 0)); 181 } 182 else //ジャンプ中 183 { 184 // ジャンプ時の処理 185 switch(n_JumpDirection) 186 { 187 //垂直跳び時 188 case 0: 189 if (hori < 0) 190 { 191 n_JumpDirection = -3; 192 } 193 if (hori > 0) 194 { 195 n_JumpDirection = 3; 196 } 197 break; 198 case -1: 199 hori = -1.0f; 200 rb.MovePosition(transform.position + new Vector3(hori * Time.deltaTime * f_Speed, 0, 0)); 201 transform.rotation = Quaternion.LookRotation(new Vector3(-90, 0, 0)); 202 203 if (hori < 0) 204 { 205 n_JumpDirection = -4; 206 } 207 break; 208 case 1: 209 hori = 1.0f; 210 rb.MovePosition(transform.position + new Vector3(hori * Time.deltaTime * f_Speed, 0, 0)); 211 transform.rotation = Quaternion.LookRotation(new Vector3(90, 0, 0)); 212 213 if (hori > 0) 214 { 215 n_JumpDirection = 4; 216 } 217 break; 218 // タグ"Wall"に当たった時の処理 219 case -2: 220 transform.rotation = Quaternion.LookRotation(new Vector3(-90, 0, 0)); 221 break; 222 case 2: 223 transform.rotation = Quaternion.LookRotation(new Vector3(90, 0, 0)); 224 break; 225 // 垂直跳び時のプレイヤー操作 226 case -3: 227 hori = -1.0f; 228 rb.MovePosition(transform.position + new Vector3(hori * Time.deltaTime * f_Speed, 0, 0)); 229 transform.rotation = Quaternion.LookRotation(new Vector3(-90, 0, 0)); 230 231 //if (hori < 0) 232 //{ 233 // n_JumpDirection = -4; 234 //} 235 break; 236 case 3: 237 hori = 1.0f; 238 rb.MovePosition(transform.position + new Vector3(hori * Time.deltaTime * f_Speed, 0, 0)); 239 transform.rotation = Quaternion.LookRotation(new Vector3(90, 0, 0)); 240 241 //if (hori > 0) 242 //{ 243 // n_JumpDirection = 4; 244 //} 245 break; 246 case -4: 247 if (hori > 0) 248 { 249 hori = -0.5f; 250 } 251 rb.MovePosition(transform.position + new Vector3(hori * Time.deltaTime * f_Speed, 0, 0)); 252 transform.rotation = Quaternion.LookRotation(new Vector3(-90, 0, 0)); 253 break; 254 case 4: 255 if (hori < 0) 256 { 257 hori = 0.5f; 258 } 259 rb.MovePosition(transform.position + new Vector3(hori * Time.deltaTime * f_Speed, 0, 0)); 260 transform.rotation = Quaternion.LookRotation(new Vector3(90, 0, 0)); 261 break; 262 default: 263 break; 264 } 265 } 266 267 //プレイヤーの位置を更新する 268 v_PlayerPos = transform.position; 269 } 270 271 void OnCollisionEnter(Collision col) 272 { 273 if (col.gameObject.tag == "Ground") 274 { 275 // 地面に付いていないときは飛べないようにする 276 if (!b_Ground) 277 { 278 b_Ground = true; 279 animator.ResetTrigger("Jump"); 280 } 281 } 282 283 if (col.gameObject.tag == "Wall") 284 { 285 if (n_JumpDirection == -1) 286 { 287 hori = 0.0f; 288 n_JumpDirection = -2; 289 } 290 if (n_JumpDirection == 1) 291 { 292 hori = 0.0f; 293 n_JumpDirection = 2; 294 } 295 } 296 297 if (col.gameObject.tag == "Carry") 298 { 299 state = "PUSH"; 300 animator.SetFloat("Run", 0.0f); 301 } 302 303 if (col.gameObject.tag == "Goal") 304 { 305 state = "CLEAR"; 306 animator.SetFloat("Run", 0.0f); 307 } 308 } 309}

試したこと

・floatではなくtriggerでやってみる
→できませんでした。

・OnCollisionStayでアニメーションの遷移する部分だけ処理してみる
→できませんでした。

・本当にアニメーションファイルは使えるかの確認(Set as Layer Default StateにしてAnimatorを見てみる)
→ちゃんとアニメーションしていました。

補足情報(FW/ツールのバージョンなど)

Unityのバージョン:2020.2.0b2.3094 Personal

・Clearはゴールオブジェクトに当たった時しか遷移しないので一方向側からしか線を出していないです。

・下がAnimatorの画像です。Idle、Jump、Runのアニメーション遷移は上手いこといっています。ClearとPushが上手くいっていません。
Animatorの写真

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ChangeAnimationが呼ばれる前にGetInputKeyとChangeStateが呼ばれているのでstateの値が書き換わっています。

投稿2021/04/15 23:28

f-rank

総合スコア132

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

.com

2021/04/16 01:33

Pushには遷移できましたが、Clearには遷移してくれません。 あと、当たり判定でPushとClearの遷移のStateを変えるのはOnCollisionStayでやることにしました。
f-rank

2021/04/16 08:15

遷移インスペクターのTransrationsタブでClearへの遷移の優先順位を確認してみてください。 Transrationsタブで上にあるほうが優先順位が高いです。
.com

2021/04/17 06:37

返信遅くなりました。 一番上にしても変わりませんでした。 また、Triggerだからダメなのかなと思ってFloatに変えてみましたが、処理は変わりませんでした。
f-rank

2021/04/17 07:18

もしスクリプトを変更しているようでしたら質問のソースコードを最新のものに更新していただけると助かります。 また、すでに確認済みだと思いますが、一応ゴールオブジェクト側の設定も確認してみてください。
.com

2021/04/17 14:11

了解しました。 ベースは別の友人が作っているので確認取ります。
.com

2021/04/20 06:31

IsTriggerがオンになっていて、オフにしたらDebugLogで当たったという処理はされました。ですが、アニメーションは流れませんでした。理由は純粋にアニメーションの変数の名前とスクリプトの名前が違ったからでした。すみません。 次に、IsTriggerをオフにしたときシーン遷移はするけどアニメーションしない。オンにしたらアニメーションするけどシーン遷移しないという問題が発生しました。 その理由はプレイヤーのアニメーションはOnCollision()で処理していて、シーン遷移はOnTriggerEnter()で処理していたからでした。 シーン遷移の処理をOnCollision()にすることで解決しました。 4日間に亘ってありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問