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

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

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

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

Unity

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

Q&A

0回答

535閲覧

Unity C# 歩行アニメーションが機能しない

退会済みユーザー

退会済みユーザー

総合スコア0

C#

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

Unity

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

0グッド

0クリップ

投稿2021/07/09 18:49

編集2021/07/09 18:51

前提・実現したいこと

歩行アニメーションを機能させたい

該当のソースコード

c#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Unit_Change_DirectionSprite : MonoBehaviour 6{ 7 public Material frontMat; 8 public Material sideMat; 9 public Material backMat; 10 11 public void Change_DirectionSprite(Vector2 posA,Vector2 posB) 12 { 13 Vector2 up = new Vector2(posA.x, posA.y + 1); 14 Vector2 down = new Vector2(posA.x, posA.y - 1); 15 Vector2 right = new Vector2(posA.x + 1, posA.y); 16 Vector2 left = new Vector2(posA.x - 1, posA.y); 17 18 Vector2 right_Diagonal_Up = new Vector2(posA.x + 1, posA.y + 1); 19 Vector2 right_Diagonal_Down = new Vector2(posA.x + 1, posA.y - 1); 20 Vector2 left_Diagonal_Up = new Vector2(posA.x - 1, posA.y + 1); 21 Vector2 left_Diagonal_Down = new Vector2(posA.x - 1, posA.y - 1); 22 23 Set_Mat(posA, posB, up, backMat); 24 Set_Mat(posA, posB, down, frontMat); 25 Set_Mat(posA, posB, right, sideMat); 26 Set_Mat(posA, posB, left, sideMat); 27 Set_Mat(posA, posB, right_Diagonal_Up, backMat); 28 Set_Mat(posA, posB, right_Diagonal_Down, frontMat); 29 Set_Mat(posA, posB, left_Diagonal_Up , backMat); 30 Set_Mat(posA, posB, left_Diagonal_Down, frontMat); 31 } 32 33 void Set_Mat(Vector2 posA,Vector2 posB,Vector2 direction,Material mat) 34 { 35 MeshRenderer meshRenderer = gameObject.GetComponent<MeshRenderer>(); 36 37 if (posB == direction) 38 { 39 meshRenderer.material = mat; 40 } 41 42 Vector2 right = new Vector2(posA.x + 1, posA.y); 43 Vector2 left = new Vector2(posA.x - 1, posA.y); 44 45 if (posB == right) 46 { 47 gameObject.transform.localScale = new Vector3(-1, 1, 1); 48 } 49 if (posB == left) 50 { 51 gameObject.transform.localScale = new Vector3(1, 1, 1); 52 } 53 } 54} 55

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Unit_RandomWalk : MonoBehaviour 6{ 7 private int map_Width = 200; 8 private int map_Height = 200; 9 10 ChessboardPathFinder pathFinder; 11 12 List<Vector2> moveList = new List<Vector2>(); 13 14 private Coroutine randamWalk_Coroutine; 15 16 private int count; 17 18 private float speed = 4f; 19 20 GameSpeed_Pause gameSpeed_Pause; 21 22 Unit_Change_DirectionSprite unit_Change_DirectionSprite; 23 24 void Start() 25 { 26 gameSpeed_Pause = GameObject.Find("Module_GameSpeed").GetComponent<GameSpeed_Pause>(); 27 28 pathFinder = new ChessboardPathFinder(map_Width, map_Height); 29 30 int randomX = Random.Range(90, 110); 31 int randomY = Random.Range(90, 110); 32 33 Vector2Int startPosition = new Vector2Int((int)gameObject.transform.position.x, (int)gameObject.transform.position.y); 34 Vector2Int goalPosition = new Vector2Int(randomX, randomY); 35 36 bool pathFound = pathFinder.FindPath(startPosition, goalPosition); 37 38 if (pathFound) 39 { 40 Add_MoveList_FoundRoute(); 41 randamWalk_Coroutine = StartCoroutine(Move()); 42 Stop_RandomWalk(); 43 } 44 else 45 { 46 Debug.LogError("Path not found."); 47 } 48 49 unit_Change_DirectionSprite = gameObject.GetComponent<Unit_Change_DirectionSprite>(); 50 } 51 52 private void Update() 53 { 54 unit_Change_DirectionSprite.Change_DirectionSprite((Vector2)gameObject.transform.position, moveList[count]); 55 } 56 57 IEnumerator Move() 58 { 59 while (true) 60 { 61 gameObject.transform.position = Vector2.MoveTowards(gameObject.transform.position, moveList[count], speed * Time.deltaTime); 62 63 if (count < moveList.Count - 1) 64 { 65 count++; 66 } 67 68 if (gameObject.transform.position == (Vector3)moveList[moveList.Count - 1]) 69 { 70 Invoke("RandomWalk", 0.5f); 71 break; 72 } 73 74 yield return null; 75 } 76 } 77 78 void Add_MoveList_FoundRoute() 79 { 80 foreach (Vector2Int position in pathFinder.Path) 81 { 82 Vector2 pos = new Vector3(position.x, position.y); 83 moveList.Add(pos); 84 } 85 } 86 87 void RandomWalk() 88 { 89 moveList.Clear(); 90 Vector2Int startPosition = new Vector2Int((int)gameObject.transform.position.x, (int)gameObject.transform.position.y); 91 int randomX = Random.Range(90, 110); 92 int randomY = Random.Range(90, 110); 93 Vector2Int goalPosition = new Vector2Int(randomX, randomY); 94 95 bool pathFound = pathFinder.FindPath(startPosition, goalPosition); 96 97 // 経路が見つかったなら移動を行う 98 if (pathFound) 99 { 100 count = 0; 101 Add_MoveList_FoundRoute(); 102 103 if(gameSpeed_Pause.paused) 104 { 105 randamWalk_Coroutine = StartCoroutine(Move()); 106 } 107 } 108 else 109 { 110 Debug.LogError("Path not found."); 111 } 112 } 113 114 public void Stop_RandomWalk() 115 { 116 if (randamWalk_Coroutine != null) 117 { 118 StopCoroutine(randamWalk_Coroutine); 119 } 120 } 121 122 public void Start_RandomWalk() 123 { 124 if (randamWalk_Coroutine != null) 125 { 126 randamWalk_Coroutine = StartCoroutine(Move()); 127 } 128 } 129} 130

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Unit_Movement : MonoBehaviour 6{ 7 private Coroutine coroutine; 8 9 [System.NonSerialized] 10 public Vector2Int goalPosition; 11 12 private ChessboardPathFinder pathFinder; 13 private int map_Width = 200; 14 private int map_Height = 200; 15 16 List<Vector2> moveList = new List<Vector2>(); 17 18 private int count; 19 20 private float speed = 4f; 21 22 [System.NonSerialized] 23 public GameObject destoryObject; 24 25 Unit_Change_DirectionSprite unit_Change_DirectionSprite; 26 27 private void Awake() 28 { 29 pathFinder = new ChessboardPathFinder(map_Width, map_Height); 30 unit_Change_DirectionSprite = gameObject.GetComponent<Unit_Change_DirectionSprite>(); 31 } 32 33 private void Update() 34 { 35 if(moveList.Count > 0) 36 { 37 unit_Change_DirectionSprite.Change_DirectionSprite((Vector2)gameObject.transform.position, moveList[count]); 38 } 39 } 40 41 public void StartMove() 42 { 43 Vector2Int startPosition = new Vector2Int((int)gameObject.transform.position.x, (int)gameObject.transform.position.y); 44 bool pathFound = pathFinder.FindPath(startPosition, goalPosition); 45 46 if (pathFound) 47 { 48 foreach (Vector2Int position in pathFinder.Path) 49 { 50 Vector2 pos = new Vector3(position.x, position.y); 51 moveList.Add(pos); 52 } 53 54 if(coroutine == null) 55 { 56 coroutine = StartCoroutine(Move()); 57 } 58 } 59 else 60 { 61 Debug.Log("目的地が見つかりません"); 62 } 63 } 64 65 private IEnumerator Move() 66 { 67 while(true) 68 { 69 gameObject.transform.position = Vector2.MoveTowards(gameObject.transform.position, moveList[count], speed * Time.deltaTime); 70 71 if (count < moveList.Count - 1) 72 { 73 count++; 74 } 75 76 if (gameObject.transform.position == (Vector3)moveList[moveList.Count - 1]) 77 { 78 Destroy(destoryObject); 79 break; 80 } 81 82 yield return null; 83 } 84 } 85} 86

試したこと

上記3つのスクリプトが、ユニットのプレハブにアタッチされています。
Unit_Change_DirectionSprite.csでユニットのアニメーションを変更しています。
ですが、Unit_RandomWalk.csでは機能するのに、Unit_Movement.csではアニメーションが機能しません。
何度デバックしても原因が分からず、困っています。回答よろしくお願いいたします。

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問