回数制限を設けて、生成時に上昇値を+し、Destroy後に上昇した値を-する。というような仕組みを考えています。
ですが、-にする処理が思った通りに動いてくれませんでした。解決案みたないなものを考えていますが、あまり良くない。そもそも間違っていると思うので、解決方法をご教授して頂けると嬉しいです。
考えているスクリプト
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
c#
1public int tsumikiLimit;//限界値 2public int tsumikiPoint;//上昇値 3 4 5void Stat() 6 { 7 tsumikiPoint = 0;//限界値初期化 8 } 9 10void Update() 11 { 12 tsumiki(); 13 } 14 15void tsuimiki() 16 { 17 tsumikiPoint ++; 18 if(tsumikiLimit < tsumikiPoint) 19 { 20 //生成処理 21 } 22 else 23 { 24 Destory(tsumikiObj);//積み木消去 25 tsumikiPoint--; 26 } 27 }
実際に実装してみたスクリプト
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.SceneManagement; 5 6 7public class Player : MonoBehaviour 8{ 9 #region Variable Declaration 10 [SerializeField] 11 public int MAXJumpCounter;//ジャンプ回数制限数設定用変数(固定数) 12 public int JumpCounterPoint;//ジャンプ回数上昇値設定用変数 13 public float jumpPower;//Player跳躍力変数 14 public float DestroyTsumikiTimer;//積み木削除時間計測用変数 15 public GameObject tsumikiPrefabs;//積み木Prefabs格納 16 public Vector2 tsumikiGeneratePosition = new Vector2();//積み木生成位置 17 public float Playerspeed; 18 public int MAX_S; 19 [Range(0, 100)] public float PlayerMaxspeed;//プレイヤー移動制限最大値変数 20 [Range(0, 100)] public int level; 21 public bool isGround; 22 public int tsumiki_GenerateLimit;//積み木生成限界数変数 23 public int tsumiki_GeneratePoint;//積み木生成個数宣言用変数 24 25 private bool tsumikiParentFlag; 26 private Rigidbody2D rg2d;//コンポーネント取得用変数 27 private float moveX = 1; 28 private float moveY; 29 private Rigidbody2D rb; 30 private GameObject tsumikiObj; 31 #endregion 32 #region Start 33 // Use this for initialization 34 void Start() 35 { 36 tsumiki_GeneratePoint = 0; 37 JumpCounterPoint = 0; 38 isGround = false; 39 rb = GetComponent<Rigidbody2D>(); 40 } 41 #endregion 42 #region Update 43 private void Update() 44 { 45 46 47 //moveY = Input.GetAxis("Vertical") * Playerspeed; 48 //Vector3 direction = new Vector2(moveX, 0); 49 tsumiki(); 50 PlayerMove(); 51 GameObjectDestroy(); 52 } 53 #endregion 54 #region Fixed 55 private void FixedUpdate() 56 { 57 58 //rb.velocity = new Vector3(moveX, 0); 59 //関数呼び出し 60 61 62 } 63 #endregion 64 #region playermove 65 private void PlayerMove() 66 { 67 //if(Input.GetAxisRaw("Horizontal")<0) 68 //{ 69 // Playerspeed = 1; 70 //}else 71 //{ 72 // Playerspeed = -1; 73 //} 74 //ADキーで移動 75 float x = Input.GetAxisRaw("Horizontal") * Playerspeed; 76 rb.velocity = (new Vector2(x, rb.velocity.y)); 77 if (x >= PlayerMaxspeed) 78 { 79 //MAX値のまま移動する処理 80 // Debug.Log("MAX!!"); 81 x = MAX_S; 82 } 83 Vector3 scale = transform.localScale; 84 if (x >= 0) 85 { 86 scale.x = 4;//右向き 87 } 88 else 89 90 { 91 scale.x = -4;//左向き 92 } 93 transform.localScale = scale; 94 95 //Wキーでジャンプする 96 if (Input.GetKeyDown(KeyCode.W)) 97 { 98 // 99 JumpCounterPoint++;//回数上昇 100 //回数制限設定変数が回数値よりも上に達した場合false 101 if (MAXJumpCounter > JumpCounterPoint) 102 { 103 // rb.velocity = Vector2.zero; 104 rb.AddForce(Vector2.up * jumpPower); 105 } 106 // Debug.Log("Jump"); 107 } 108 } 109 #endregion 110 #region tsumiki 111 private void tsumiki() 112 { 113 // Spaceキーを入力されたら、積み木Prefabを複製する。 114 if (Input.GetKeyDown(KeyCode.Space)) 115 { 116 117 transform.position += new Vector3(0f, 0.5f, 0f); 118 Vector2 position = transform.position + transform.up * tsumikiGeneratePosition.y + transform.right * tsumikiGeneratePosition.x; 119 tsumiki_GeneratePoint++;//生成時に積み木生成個数増加 120 121 if (tsumiki_GenerateLimit > tsumiki_GeneratePoint)//限界値達成したら通らない 122 { 123 tsumikiObj = Instantiate(tsumikiPrefabs, position, Quaternion.identity); //積み木Prefabの複製 124 tsumikiParentFlag = true; 125 Debug.Log("true"); 126 var Tsumiki_Pos = transform.localPosition;//Player座標取得 127 128 tsumikiObj.transform.parent = GameObject.Find("Player").transform;//積み木Prefabを親子関係に 129 Debug.Log("くっついた!"); 130 131 } 132 133 } 134 else if (Input.GetKeyUp(KeyCode.Space)) 135 { 136 tsumikiParentFlag = false; 137 tsumikiObj.transform.parent = null;//親子関係解除 138 Debug.Log("解除"); 139 } 140 } 141#endregion 142 private void GameObjectDestroy() 143 { 144 145 Destroy(tsumikiObj, DestroyTsumikiTimer);//設定時間到達後積み木削除 146 tsumiki_GeneratePoint = -1;//積み木生成数を-していく 147 148 } 149#region 150 private void OnCollisionEnter2D(Collision2D collision) 151 { 152 if (collision.gameObject.name == "DeadPoint") 153 { 154 Life.LIFE_SYSTEM -= 3; 155 156 // Destroy(gameObject); 157 } 158 if(collision.gameObject.name == "ClearPoint") 159 { 160 SceneManager.LoadScene("GameClear"); 161 } 162 if(collision.gameObject.tag =="Ground") 163 { 164 JumpCounterPoint = 0; 165 } 166 } 167} 168#endregion 169 170
回答1件
あなたの回答
tips
プレビュー