using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class PlayerShootAction : MonoBehaviour { Slider slider; RectTransform rect; [SerializeField] GameObject shootGauge; //シュート成功率ゲージ private float shootControlGauge = 0.02f; //ゲージの加算度 private bool isGaugeUpdateOK = false; //シュート開始時の処理を許可(ButtonEventから変更される) private Vector3 randPos; //確立によるゴールターゲット変更用 private bool isShootSuccess = false; //シュート成功判定 void Start() { slider = shootGauge.GetComponent(); shootGauge.SetActive(false); } void Update() { if (!isGaugeUpdateOK) return; //ゲージ加算 slider.value += shootControlGauge; Debug.Log("ゲージ加算"); } //シュートの許可 public void IsGaugeUpdate(bool b) { isGaugeUpdateOK = b; } //ButtonEventでシュートのボタンが離された時 //ゲージの量でシュート成功の判定を行う public void GaugeJudge() { //ゲージが一定範囲内なら成功判定 if (slider.value >= 0.75f && slider.value <= 0.85f) { isShootSuccess = true; //Debug.Log("isShootSuccess = " + isShootSuccess); return; } isShootSuccess = false; //Debug.Log("isShootSuccess = " + isShootSuccess); //Debug.Log("gauge = " + slider.value); float parsent = 0f;//確率 0.0f~100.0f //ゲージの値を確立へ //0が0% 0.8に近づくにつれて確率が上がり、0.8がゴール成功率100% 0.8から1に近づくにつれて確率が下がり、1が0% //しかし、ゴールポストにかなり近い場合、ボタンを押すだけでシュートする(確率なし シュート成功率80%or90%) parsent = slider.value * 100f / 0.8f; //0.8が100になるように補正 //0.8を超えた場合、1に近づくにつれて確率を下げる if (parsent > 100) { parsent = 70f - (parsent - 100f); } //parsentを0から10にする parsent /= 10f; if (slider.value < 0.75f) { randPos = new Vector3(Random.Range(0, 8f) / parsent * 1.5f, 0, Random.Range(0, 8f) / parsent * 1.5f); } else { randPos = new Vector3(Random.Range(0, 8f) / (slider.value * 10f) * 1.5f, 0, Random.Range(0, 8f) / (slider.value * 10f) * 1.5f); } randPos.x = -randPos.x; /* //本来狙うゴールに、確率で算出した狙う位置がかなり近い場合、シュート成功判定を出す。 if (Vector3.Distance(randPos + GetComponent().getAimGoalPostPosition(), GetComponent().getAimGoalPostPosition()) <= 0.2f) { isShootSuccess = true; } */ } public void gaugeActive(bool b) { shootGauge.SetActive(b); } //ゴールを狙うゲージアクション開始・終了、同時にゲージの値の初期化 public void toDecideAim(bool b) { shootGauge.SetActive(b); isGaugeUpdateOK = b; //Debug.Log("PlayerShoot toDecideAim = " + b); slider.value = 0f; } //Playerへ確率で算出したゴールの位置とのずれを返す //(Playerでゴールポストの位置にプラスして狙う位置をずれさせる) public Vector3 getShootEndPoint() { return randPos; } //Playerへシュート成功判定を返す public bool getShootSuccess() { return isShootSuccess; } //ゲージをプレイヤーの位置の近くに合わせる public void setGaugePosition(Transform playerTrans) { Vector2 pos = Camera.main.WorldToScreenPoint(playerTrans.position); //rect.position = RectTransformUtility.WorldToScreenPoint(Camera.main, playerTrans.position); pos = new Vector2(pos.x + 30f, pos.y); shootGauge.transform.position = pos; } }