C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class PlayerControlScript : MonoBehaviour { 6 7 MapGenerator mapGenerator; 8 GameObject cat; 9 Vector3 catpos; 10 private Vector3 nowpos; 11 private Animator animator; 12 13 // Use this for initialization 14 void Start() 15 { 16 cat = GameObject.Find("MapGen"); 17 mapGenerator = cat.GetComponent<MapGenerator>(); 18 animator = GetComponent<Animator>(); 19 } 20 21 // Update is called once per frame 22 void Update() 23 { 24 catpos = transform.position; 25 26 // 移動処理 27 Move(); 28 } 29 30 // 移動関数 31 void Move() 32 { 33 // 左キーを押し続けていたら 34 if (Input.GetKey("left")) 35 { 36 foreach (Vector3 pos in mapGenerator.listpos) 37 { 38 Vector3 left = new Vector3(catpos.x - 1, catpos.y, 0); 39 if (left == pos) 40 { 41 animator.SetTrigger("Left"); 42 Vector3 scale = transform.localScale; 43 scale.x = 1; 44 transform.localScale = scale; 45 nowpos = new Vector3(catpos.x - 1f, catpos.y + 0.5f, 0); 46 iTween.MoveTo(gameObject, nowpos, 0.2f); 47 } 48 } 49 } 50 51 if (Input.GetKey("right")) 52 { // 右キーを押し続けていたら 53 foreach (Vector3 pos in mapGenerator.listpos) 54 { 55 Vector3 right = new Vector3(catpos.x + 1, catpos.y - 1, 0); 56 if(right == pos) 57 { 58 animator.SetTrigger("Down"); 59 Vector3 scale = transform.localScale; 60 scale.x = -1; 61 transform.localScale = scale; 62 nowpos = new Vector3(catpos.x + 1f, catpos.y - 0.5f, 0); 63 iTween.MoveTo(gameObject, nowpos, 0.2f); 64 } 65 } 66 } 67 if (Input.GetKey("up")) 68 { // 上キーを押し続けていたら 69 foreach (Vector3 pos in mapGenerator.listpos) 70 { 71 Vector3 up = new Vector3(catpos.x + 1, catpos.y, 0); 72 if(up == pos) 73 { 74 animator.SetTrigger("Left"); 75 Vector3 scale = transform.localScale; 76 scale.x = -1; 77 transform.localScale = scale; 78 nowpos = new Vector3(catpos.x + 1f, catpos.y + 0.5f, 0); 79 iTween.MoveTo(gameObject, nowpos, 0.2f); 80 } 81 } 82 } 83 84 if (Input.GetKey("down")) 85 { // 下キーを押し続けていたら 86 foreach (Vector3 pos in mapGenerator.listpos) 87 { 88 Vector3 down = new Vector3(catpos.x - 1, catpos.y - 1, 0); 89 if(down == pos) 90 { 91 animator.SetTrigger("Down"); 92 Vector3 scale = transform.localScale; 93 scale.x = 1; 94 transform.localScale = scale; 95 nowpos = new Vector3(catpos.x - 1f, catpos.y - 0.5f, 0); 96 iTween.MoveTo(gameObject, nowpos, 0.2f); 97 } 98 } 99 } 100 101 } 102} 103
行動を終えたとき、DownIdleまたはLeftIdleに移行するようにしたいのですが、上手くいきません。
C#
1if (Input.GetKey("left")) 2 { 3 foreach (Vector3 pos in mapGenerator.listpos) 4 { 5 Vector3 left = new Vector3(catpos.x - 1, catpos.y, 0); 6 if (left == pos) 7 { 8 animator.SetTrigger("Left"); 9 Vector3 scale = transform.localScale; 10 scale.x = 1; 11 transform.localScale = scale; 12 nowpos = new Vector3(catpos.x - 1f, catpos.y + 0.5f, 0); 13 iTween.MoveTo(gameObject, nowpos, 0.2f); 14 } 15 } 16 } 17 else 18 { 19 animator.SetTrigger("LeftIdle"); 20 } 21 if (Input.GetKey("right")) 22 { // 右キーを押し続けていたら 23 foreach (Vector3 pos in mapGenerator.listpos) 24 { 25 Vector3 right = new Vector3(catpos.x + 1, catpos.y - 1, 0); 26 if(right == pos) 27 { 28 animator.SetTrigger("Down"); 29 Vector3 scale = transform.localScale; 30 scale.x = -1; 31 transform.localScale = scale; 32 nowpos = new Vector3(catpos.x + 1f, catpos.y - 0.5f, 0); 33 iTween.MoveTo(gameObject, nowpos, 0.2f); 34 } 35 } 36 } 37 else 38 { 39 animator.SetTrigger("DownIdle"); 40 }
というようにしていくと、LeftIdleとDownIdleが同時に起こるので、それ以外のアニメーションに移行しなくなります。
Input.GetKeyUpしたときのみ、LeftIdleもしくはDownIdleにしようとしましたが、立ち止まったり歩いたりという変な挙動になってしまいます。エラーなどではなく、GetKeyUpが思うように反応していないようです。
歩き終わったあとは、DownIdleまたはLeftIdleに切り替わり、方向キーを押せば、歩き出すようにしたいのですが、方法が思いつきません。やり方を教えてもらえるとありがたいです。
回答よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。