using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player : MonoBehaviour
{
public float playerHealth = 100f;
public float currentHealth = 100f;
public float playerScore = 0f;
public float passScore = 60f;
public float fireRate = 0.4f;
public float verticalRecoil = 5f;
public float horizontalRecoil = 2f;
public float recoilTime = 0.02f;
public float recoilReduceTime = 0.5f;
public GameObject scoreDialog;
public Text scoreDialogText;
public static Player Instance;
private Image hpSlider;
private Text scoreText;
private CameraController cameraController;
private Animator weaponAnimator;
private AudioSource weaponAudio;
[SerializeField]
private Transform shootPoint;
//private ParticleSystem[] flareArray;
private ManagerVars vars;
[SerializeField]
private float fireTimer;
private void Awake()
{
Instance = this;
vars = ManagerVars.GetManagerVars();
hpSlider = GameObject.Find("HpSlider").GetComponent<Image>();
scoreText = GameObject.Find("ScoreText").GetComponent<Text>();
cameraController = GameObject.Find("OVRCameraRig").GetComponent<CameraController>();
weaponAnimator = GameObject.Find("Shotgun").GetComponent<Animator>();
weaponAudio = GameObject.Find("Shotgun").GetComponent<AudioSource>();
//flareArray = GameObject.Find("Flare").GetComponentsInChildren<ParticleSystem>();
fireTimer = fireRate;
gameObject.SetActive(false);
}
private void Update()
{
if (playerScore > 0)
{
scoreText.text = "分數:" + playerScore;
}
else
{
scoreText.text = "";
}
currentHealth = Mathf.Clamp(currentHealth -= Time.deltaTime, 0f, playerHealth);
if (currentHealth <= 0)
{
if (playerScore >= passScore)
{
scoreDialogText.text = playerScore + "/" + passScore + "\r\n" + "挑戰成 功";
}
else
{
scoreDialogText.text = playerScore + "/" + passScore + "\r\n" + "挑戰失 敗";
}
scoreDialog.GetComponent<DialogEffect>().isPlayEffect = true;
gameObject.SetActive(false);
}
hpSlider.fillAmount = currentHealth / playerHealth;
if (Input.GetButtonDown("Fire1"))
{
cameraController.StartFire();
}
if (Input.GetButtonUp("Fire1"))
{
cameraController.EndFire(recoilReduceTime);
}
if (fireTimer > fireRate)
{
if (Input.GetButton("Fire1"))
{
fireTimer = 0;
Fire();
}
}
else
{
fireTimer += Time.deltaTime;
}
}
private void Fire()
{
weaponAnimator.CrossFadeInFixedTime("Fire", 0.001f);
foreach (ParticleSystem temp in flareArray)
{
temp.Play();
}
weaponAudio.PlayOneShot(vars.WeaponClip);
GameObject bullet = ObjectPool.Instance.GetObject(vars.BulletPrefab,ref ObjectPool.Instance.Bulletlist);
bullet.transform.position = shootPoint.position;
bullet.transform.localEulerAngles = shootPoint.eulerAngles;
Debug.Log("aaaaa");
bullet.SetActive(true);
StartCoroutine(cameraController.AddVerticalRecoil(verticalRecoil, recoilTime));
if (Random.Range(0f, 10f) > 5f)
{
StartCoroutine(cameraController.AddHorizontalRecoil(horizontalRecoil, recoilTime));
}
else
{
StartCoroutine(cameraController.AddHorizontalRecoil(-horizontalRecoil, recoilTime));
}
}
private void Start()
{
shootPoint = GameObject.FindWithTag("ShootPoint").transform;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/20544.html
標籤:Unity3D
