Player.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Player : MonoBehaviour
  5. {
  6. [SerializeField]
  7. private float powerLevel;
  8. public float PowerLevel
  9. {
  10. get
  11. {
  12. return powerLevel;
  13. }
  14. set
  15. {
  16. powerLevel = value;
  17. UpdatePowerBar();
  18. if (powerLevel > powerLevelMax)
  19. {
  20. powerLevel = powerLevelMax;
  21. }
  22. }
  23. }
  24. private void UpdatePowerBar()
  25. {
  26. var level = powerLevel / powerLevelMax;
  27. gauge.localScale = new Vector3(1, level, 1);
  28. }
  29. public float startSpeed = 5f;
  30. public float lookSmoothingSeconds = 0.05f;
  31. public float cameraSmoothingSeconds = 1;
  32. public float bulletUpBias = 0.1f;
  33. public float bulletForce = 10f;
  34. public float powerLevelMax = 8.0f;
  35. public Transform gunTransform;
  36. public Rigidbody bulletPrefab;
  37. public Light muzzleLight;
  38. public Transform gauge;
  39. private Vector3 inputDirection;
  40. private float speed;
  41. private Vector3 lookDirection;
  42. private float deadZone = 0.1f;
  43. private Vector3 lookVelocity;
  44. private Vector3 cameraOffset;
  45. private Vector3 cameraVelocity;
  46. private bool shooting = false;
  47. private float lightIntensity;
  48. // Use this for initialization
  49. void Start ()
  50. {
  51. speed = startSpeed;
  52. cameraOffset = Camera.main.transform.position - transform.position;
  53. lightIntensity = muzzleLight.intensity;
  54. }
  55. // Update is called once per frame
  56. void Update ()
  57. {
  58. // TODO lerp movement direction
  59. GetInput();
  60. if (lookDirection.magnitude > deadZone)
  61. transform.forward = Vector3.SmoothDamp(transform.forward, lookDirection, ref lookVelocity, lookSmoothingSeconds);
  62. transform.position += inputDirection * speed * Time.deltaTime;
  63. UpdatePowerBar();
  64. Camera.main.transform.position = Vector3.SmoothDamp(Camera.main.transform.localPosition, TargetCameraPosition(), ref cameraVelocity, cameraSmoothingSeconds);
  65. if (shooting)
  66. {
  67. if (Time.frameCount % 2 == 0)
  68. {
  69. muzzleLight.intensity = 0;
  70. }
  71. else
  72. {
  73. muzzleLight.intensity = Random.Range(.5f,2f) * lightIntensity;
  74. }
  75. Shoot();
  76. }
  77. else
  78. {
  79. muzzleLight.intensity = 0;
  80. }
  81. }
  82. private void Shoot()
  83. {
  84. var rb = Instantiate<Rigidbody>(bulletPrefab);
  85. rb.transform.position = gunTransform.position;
  86. rb.transform.forward = gunTransform.forward;
  87. rb.AddForce(bulletForce * (transform.forward + Vector3.up * bulletUpBias), ForceMode.VelocityChange);
  88. Destroy(rb.gameObject, 2f);
  89. }
  90. private Vector3 TargetCameraPosition()
  91. {
  92. return transform.position + cameraOffset;
  93. }
  94. void GetInput()
  95. {
  96. shooting = Input.GetButton("Fire1");
  97. lookDirection = new Vector3(
  98. Input.GetAxis("LookX"),
  99. 0,
  100. -Input.GetAxis("LookY")
  101. ).normalized;
  102. inputDirection = new Vector3(
  103. Input.GetAxis("Horizontal"),
  104. 0,
  105. Input.GetAxis("Vertical")
  106. ).normalized;
  107. }
  108. }