###実現したいこと
Box Collider2Dで当たったらSpriteを変える
できればUnity側でSpriteを固定してその画像に変える
###該当のソースコード
C#
1public class Player : MonoBehaviour { 2 3 SpriteRenderer MainSpriteRenderer; 4 // Spaceshipコンポーネント 5 Spaceship spaceship; 6 7 public Sprite onestage; 8 public Sprite twostep; 9 public Sprite Threestages; 10 11 12 IEnumerator Start() 13 { 14 15 MainSpriteRenderer = gameObject.GetComponent<SpriteRenderer>(); 16 17 18 } 19 20 void Update() 21 { 22 23 } 24 void OnTriggerExit2D(Collider2D other) 25 { 26 if (other.tag == "item") 27 { 28 MainSpriteRenderer.sprite = twostep; 29 } 30 if (other.tag == "item") 31 { 32 twostep = Threestages; 33 } 34 35 if (other.tag == "Enemy") 36 { 37 Threestages = twostep; 38 } 39 40 if (other.tag == "Enemy") 41 { 42 twostep = onestage; 43 } 44 } 45 46}
###試したこと
void OnTriggerExit2D(Collider2D other) { if (other.tag == "item") { MainSpriteRenderer.sprite = twostep; } if (other.tag == "item") { twostep = Threestages; } if (other.tag == "Enemy") { Threestages = twostep; } if (other.tag == "Enemy") { twostep = onestage; }
の部分を
void OnTriggerExit2D(Collider2D other) { switch (other.tag) { case "item": MainSpriteRenderer.sprite = twostep; break; case "Enemy": twostep = onestage; break; } }
と書き換えて画像は確かに変わるのですがUnityのSpriteのtwostepの画像がMainSpriteRenderer.spriteに塗り替えられて敵に当たって画像が戻ってもそのままtwostepの画像は初期の画像のままで変化なしです
if文で試したところ同じ結果です。。。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/02/13 03:46
2017/02/13 03:59 編集
2017/02/13 04:38