PlayerController.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlayerController : MonoBehaviour {
  5. public static PlayerController Instance;
  6. public float MoveSpeed;
  7. private GameObject bullet;
  8. private float timecount;
  9. public float FireCD;
  10. private Transform firePos;
  11. private float time;
  12. public GameObject explode;
  13. private void Awake()
  14. {
  15. Instance = this;
  16. }
  17. // Use this for initialization
  18. void Start () {
  19. bullet = Resources.Load<GameObject>("Prefabs/Bullet");
  20. firePos = transform.Find("FirePos0");
  21. PlaneManager.instance.PlayerHP = 20;
  22. explode = Resources.Load<GameObject>("Prefabs/Explode");
  23. }
  24. // Update is called once per frame
  25. void Update () {
  26. Move();
  27. //Fire();
  28. NormalFire();
  29. // SuperFire();
  30. isDeath();
  31. }
  32. //键盘控制飞机移动 用mathf方法限制飞机移动的范围
  33. void Move()
  34. {
  35. if (Input.GetKey(KeyCode.A))
  36. {
  37. transform.Translate(Vector3.right * MoveSpeed * Time.deltaTime);
  38. }
  39. if (Input.GetKey(KeyCode.D))
  40. {
  41. transform.Translate(Vector3.left * MoveSpeed * Time.deltaTime);
  42. }
  43. if (Input.GetKey(KeyCode.W))
  44. {
  45. transform.Translate(Vector3.up * MoveSpeed * Time.deltaTime);
  46. }
  47. if (Input.GetKey(KeyCode.S))
  48. {
  49. transform.Translate(Vector3.down * MoveSpeed * Time.deltaTime);
  50. }
  51. transform.position = new Vector3(Mathf.Clamp(transform.position.x, -6.37f, 6.64f), Mathf.Clamp(transform.position.y, -6.595f, 6.595f), Mathf.Clamp(transform.position.z, -6.9f, 6.5f));
  52. }
  53. private void NormalFire()
  54. {
  55. timecount += Time.deltaTime;
  56. if (timecount >= FireCD)
  57. {
  58. GameObject go = Instantiate(bullet);
  59. go.transform.position = LookAtFj.Instance.transform.position;
  60. go.transform.eulerAngles = new Vector3( LookAtFj.Instance.transform.eulerAngles.x-180f, LookAtFj.Instance.transform.eulerAngles.y, LookAtFj.Instance.transform.eulerAngles.z);
  61. timecount = 0;
  62. }
  63. }
  64. //private void SuperFire()
  65. //{
  66. // timecount += Time.deltaTime;
  67. // if (timecount >= FireCD)
  68. // {
  69. // for (int i = 0; i < 5; i++)
  70. // {
  71. // GameObject go = Instantiate(bullet);
  72. // go.transform.position = transform.Find("FirePos" + i).position;
  73. //
  74. // }
  75. //
  76. // timecount = 0;
  77. // }
  78. //}
  79. //受伤 减血
  80. public void BeHit()
  81. {
  82. PlaneManager.instance.PlayerHP-=5 ;
  83. }
  84. private void isDeath()
  85. {
  86. if (false)//PlaneManager.instance.PlayerHP <= 0
  87. {
  88. ScoreManager.instance.SetScore();
  89. UIManager.instance.CreatePanel("PanelOver");
  90. UIManager.instance.DestroyPanel("PanelGame");
  91. CreateStone.instance.stopStone();
  92. CreateStone.instance.stopProp();
  93. Destroy(this.gameObject);
  94. }
  95. }
  96. //碰撞检测 如果碰到的stone 触发受伤方法 生成爆炸特效
  97. private void OnTriggerEnter(Collider other)
  98. {
  99. if (other.tag=="stone")
  100. {
  101. BeHit();
  102. GameObject go = Instantiate(explode);
  103. go.transform.position = other.transform.position;
  104. Destroy(other.gameObject);
  105. }
  106. //如果碰到的是道具 血量加满
  107. if (other.tag=="prop")
  108. {
  109. PlaneManager.instance.PlayerHP = 20;
  110. //PlaneManager.instance.PlayerHP +=5;
  111. //PanelGame.instance.blood.transform.localScale = new Vector3(1, 1, 1);
  112. Destroy(other.gameObject);
  113. }
  114. }
  115. }
  116. //void Fire()
  117. //{
  118. // if (Input.GetKey(KeyCode.Q))
  119. // {
  120. // NormalFire();
  121. // }
  122. // if (Input.GetKey(KeyCode.E))
  123. // {
  124. // SuperFire();
  125. // }
  126. //}