<実現したいこと>
Unityで戦闘ゲーム的なものを作っています。
タップした場所に応じてダメージが入るようにしています。
-1減るはずなのですが、100減ってしまいます。
<ソースコード>
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5using UnityEngine.SceneManagement; 6 7public class fightManagear : MonoBehaviour 8{ 9 bool mopen = true; 10 bool seikai = false; 11 public Sprite sprite; 12 public Sprite sprite1; 13 public Sprite Item; 14 public Sprite Item1; 15 public Sprite Item2; 16 public Sprite Item3; 17 public Sprite Item4; 18 public Sprite Item5; 19 public Text Lifetext; 20 public Text Text; 21 public int heart = 100; 22 public int heart2 = 900; 23 public AudioClip damageSE; 24 public AudioClip AttackSE; 25 AudioSource aud; 26 float fo = 0; 27 int i; 28 bool tap = false; 29 int color; 30 int color2; 31 // Start is called before the first frame update 32 void Start() 33 { 34 Text.text = "HP: " + 900; 35 heart2 = PlayerPrefs.GetInt("level"); 36 aud = GetComponent<AudioSource>(); 37 i = Random.Range(1, 6); 38 if (i == 1) 39 { 40 GameObject.Find("Item").GetComponent<SpriteRenderer>().sprite = Item; 41 } 42 else if (i == 2) 43 { 44 GameObject.Find("Item").GetComponent<SpriteRenderer>().sprite = Item1; 45 } 46 else if (i == 3) 47 { 48 GameObject.Find("Item").GetComponent<SpriteRenderer>().sprite = Item2; 49 } 50 else if (i == 4) 51 { 52 GameObject.Find("Item").GetComponent<SpriteRenderer>().sprite = Item3; 53 } 54 else if (i == 5) 55 { 56 GameObject.Find("Item").GetComponent<SpriteRenderer>().sprite = Item4; 57 } 58 else if (i == 6) 59 { 60 GameObject.Find("Item").GetComponent<SpriteRenderer>().sprite = Item5; 61 } 62 63 } 64 65 // Update is called once per frame 66 void Update() 67 { 68 fo += Time.deltaTime; 69 if (fo >= 1) 70 { 71 Attack(); 72 fo = 0; 73 if (mopen) 74 { 75 GameObject.Find("monster07").GetComponent<SpriteRenderer>().sprite = sprite1; 76 mopen = false; 77 } 78 else 79 { 80 GameObject.Find("monster07").GetComponent<SpriteRenderer>().sprite = sprite; 81 mopen = true; 82 } 83 84 } 85 if (heart <= 0) 86 { 87 SceneManager.LoadScene("GameOverScene"); 88 } 89 if (Input.GetMouseButtonDown(0) || Input.GetTouch(0).phase == TouchPhase.Began) 90 91 { 92 // クリック・タップした瞬間の座標 93 var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition + Camera.main.transform.forward); 94 if (pos.x <= -1.2) 95 { 96 //Debug.Log("R"); 97 tap = true; 98 color = 1; 99 } 100 else if (pos.x > 1.0) 101 { 102 //Debug.Log("B"); 103 tap = true; 104 color = 2; 105 } 106 else if (pos.y > 0.5) 107 { 108 //Debug.Log("G"); 109 tap = true; 110 color = 3; 111 } 112 i = Random.Range(1, 6); 113 if (tap) 114 { 115 if (i == 1) 116 { 117 GameObject.Find("Item").GetComponent<SpriteRenderer>().sprite = Item; 118 color2 = 1; 119 120 } 121 else if (i == 2) 122 { 123 GameObject.Find("Item").GetComponent<SpriteRenderer>().sprite = Item1; 124 color2 = 2; 125 } 126 else if (i == 3) 127 { 128 GameObject.Find("Item").GetComponent<SpriteRenderer>().sprite = Item2; 129 color2 = 1; 130 } 131 else if (i == 4) 132 { 133 GameObject.Find("Item").GetComponent<SpriteRenderer>().sprite = Item3; 134 color2 = 3; 135 } 136 else if (i == 5) 137 { 138 GameObject.Find("Item").GetComponent<SpriteRenderer>().sprite = Item4; 139 color = 2; 140 } 141 else if (i == 6) 142 { 143 GameObject.Find("Item").GetComponent<SpriteRenderer>().sprite = Item5; 144 color2 = 3; 145 } 146 clickd(); 147 } 148 149 } 150 } 151 public void Attack() 152 { 153 aud.PlayOneShot(damageSE); 154 heart -= PlayerPrefs.GetInt("level")*2; 155 Lifetext.text = "HP: " + heart; 156 } 157 public void clickd() 158 { 159 if(color==color2) 160 { 161 Debug.Log("Attack"); 162 seikai = false; 163 aud.PlayOneShot(AttackSE); 164 heart2 = heart2 - PlayerPrefs.GetInt("level"); 165 Text.text = "HP: " + heart2; 166 167 } 168 } 169}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/07 04:03
2020/06/07 04:12
2020/06/07 06:33
2020/06/07 06:35
2020/06/07 06:41
2020/06/07 06:44
2020/06/07 06:46
2020/06/07 09:29
2020/06/08 07:43
2020/06/08 13:55
2020/06/08 14:04
2020/06/09 07:44
2020/06/09 07:45