Player.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. private bool useMouseLook;
  49. // Use this for initialization
  50. void Start ()
  51. {
  52. speed = startSpeed;
  53. cameraOffset = Camera.main.transform.position - transform.position;
  54. lightIntensity = muzzleLight.intensity;
  55. var joystickNames = Input.GetJoystickNames();
  56. useMouseLook = joystickNames.Length == 0 || string.IsNullOrEmpty(joystickNames[0]);
  57. if (useMouseLook)
  58. {
  59. lookSmoothingSeconds = 0;
  60. }
  61. }
  62. // Update is called once per frame
  63. void Update ()
  64. {
  65. // TODO lerp movement direction
  66. GetInput();
  67. if (lookDirection.magnitude > deadZone)
  68. transform.forward = Vector3.SmoothDamp(transform.forward, lookDirection, ref lookVelocity, lookSmoothingSeconds);
  69. transform.position += inputDirection * speed * Time.deltaTime;
  70. UpdatePowerBar();
  71. Camera.main.transform.position = Vector3.SmoothDamp(Camera.main.transform.localPosition, TargetCameraPosition(), ref cameraVelocity, cameraSmoothingSeconds);
  72. if (shooting)
  73. {
  74. if (Time.frameCount % 2 == 0)
  75. {
  76. muzzleLight.intensity = 0;
  77. }
  78. else
  79. {
  80. muzzleLight.intensity = Random.Range(.5f,2f) * lightIntensity;
  81. }
  82. Shoot();
  83. }
  84. else
  85. {
  86. muzzleLight.intensity = 0;
  87. }
  88. }
  89. private void Shoot()
  90. {
  91. var rb = Instantiate<Rigidbody>(bulletPrefab);
  92. rb.transform.position = gunTransform.position;
  93. rb.transform.forward = gunTransform.forward;
  94. rb.AddForce(bulletForce * (transform.forward + Vector3.up * bulletUpBias), ForceMode.VelocityChange);
  95. Destroy(rb.gameObject, 2f);
  96. }
  97. private Vector3 TargetCameraPosition()
  98. {
  99. return transform.position + cameraOffset;
  100. }
  101. void GetInput()
  102. {
  103. shooting = Input.GetButton("Fire1");
  104. var padLook = new Vector3(
  105. Input.GetAxis("LookX"),
  106. 0,
  107. -Input.GetAxis("LookY")
  108. );
  109. if (useMouseLook)
  110. {
  111. var mouseLook = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
  112. mouseLook.Normalize();
  113. lookDirection = new Vector3(mouseLook.x, 0, mouseLook.y);
  114. }
  115. else if (padLook.magnitude > 0.2f)
  116. {
  117. lookDirection = padLook.normalized;
  118. }
  119. inputDirection = new Vector3(
  120. Input.GetAxis("Horizontal"),
  121. 0,
  122. Input.GetAxis("Vertical")
  123. ).normalized;
  124. }
  125. }