Player.cs 2.9 KB

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