前提・実現したいこと
unity2Dでキャラクターを歩かせるアニメーションを作ったところ、キャラクターが止まったとき下の画像のように歩いている途中のspriteで停止してしまいました。止まったときに一番最初のspriteもしくは一番最後のspriteで停止させるにはどうしたらいいのでしょうか。
試したこと
スクリプトの一番下の行で、キャラクターのx軸方向に動くスピードが0になったとき(アニメーションが停止したとき)spriteを強制的に変更させるという方法を試してみましたが、spriteは変更されませんでした。
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class KnightCtrl : MonoBehaviour { 6 7 Rigidbody2D rb; 8 Animator animator; 9 SpriteRenderer sr; 10 public Sprite stop; 11 float walkSpeed = 60; 12 float maxSpeed = 5.0f; 13 float jump = 300; 14 15 void Start () { 16 this.rb = GetComponent<Rigidbody2D>(); 17 this.animator = GetComponent<Animator>(); 18 this.sr = GetComponent<SpriteRenderer>(); 19 } 20 21 void Update () { 22 float x = Input.GetAxis("Horizontal"); 23 float speed = Mathf.Abs(this.rb.velocity.x); 24 //歩く 25 if((speed < this.maxSpeed)){ 26 rb.AddForce(transform.right * x * this.walkSpeed); 27 } 28 //ジャンプ 29 Vector2 force = new Vector2(0, this.jump); 30 if (Input.GetKeyDown(KeyCode.Space) && this.rb.velocity.y == 0) { 31 this.rb.AddForce(force); 32 } 33 34 //しゃがむ 35 if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S)){ 36 37 } 38 39 float reverse = 0; 40 if (x > 0){ 41 reverse = 1.5f; 42 }else if (x < 0){ 43 reverse = -1.5f; 44 } 45 //反転 46 if (reverse != 0){ 47 transform.localScale = new Vector3(reverse, 1.5f, 1); 48 } 49 50 this.animator.speed = speed / 2.0f; 51 52 //歩くアニメーションが停止したとき止まっているspriteに切り替える 53 if (this.rb.velocity.x == 0){ 54 this.sr.sprite = this.stop; 55 } 56 } 57} 58
補足情報(FW/ツールのバージョンなど)
使用OS : windows7
unityバージョン : Unity 2018.2.7f1 Personal
他に必要な情報があったら追記します。
回答1件
あなたの回答
tips
プレビュー