2021년 4학년 1학기 기업연계프로젝트2 컴퓨터소프트웨어공학과 <원광투어팀> 팀장 : 송유진 팀원 : 김나영, 이경희, 한유진
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.8 KiB

5 years ago
  1. using Unity.FPS.Game;
  2. using UnityEngine;
  3. namespace Unity.FPS.Gameplay
  4. {
  5. [RequireComponent(typeof(Rigidbody), typeof(Collider))]
  6. public class Pickup : MonoBehaviour
  7. {
  8. [Tooltip("Frequency at which the item will move up and down")]
  9. public float VerticalBobFrequency = 1f;
  10. [Tooltip("Distance the item will move up and down")]
  11. public float BobbingAmount = 1f;
  12. [Tooltip("Rotation angle per second")] public float RotatingSpeed = 360f;
  13. [Tooltip("Sound played on pickup")] public AudioClip PickupSfx;
  14. [Tooltip("VFX spawned on pickup")] public GameObject PickupVfxPrefab;
  15. public Rigidbody PickupRigidbody { get; private set; }
  16. Collider m_Collider;
  17. Vector3 m_StartPosition;
  18. bool m_HasPlayedFeedback;
  19. protected virtual void Start()
  20. {
  21. PickupRigidbody = GetComponent<Rigidbody>();
  22. DebugUtility.HandleErrorIfNullGetComponent<Rigidbody, Pickup>(PickupRigidbody, this, gameObject);
  23. m_Collider = GetComponent<Collider>();
  24. DebugUtility.HandleErrorIfNullGetComponent<Collider, Pickup>(m_Collider, this, gameObject);
  25. // ensure the physics setup is a kinematic rigidbody trigger
  26. PickupRigidbody.isKinematic = true;
  27. m_Collider.isTrigger = true;
  28. // Remember start position for animation
  29. m_StartPosition = transform.position;
  30. }
  31. void Update()
  32. {
  33. // Handle bobbing
  34. float bobbingAnimationPhase = ((Mathf.Sin(Time.time * VerticalBobFrequency) * 0.5f) + 0.5f) * BobbingAmount;
  35. transform.position = m_StartPosition + Vector3.up * bobbingAnimationPhase;
  36. // Handle rotating
  37. transform.Rotate(Vector3.up, RotatingSpeed * Time.deltaTime, Space.Self);
  38. }
  39. void OnTriggerEnter(Collider other)
  40. {
  41. PlayerCharacterController pickingPlayer = other.GetComponent<PlayerCharacterController>();
  42. if (pickingPlayer != null)
  43. {
  44. OnPicked(pickingPlayer);
  45. PickupEvent evt = Events.PickupEvent;
  46. evt.Pickup = gameObject;
  47. EventManager.Broadcast(evt);
  48. }
  49. }
  50. protected virtual void OnPicked(PlayerCharacterController playerController)
  51. {
  52. PlayPickupFeedback();
  53. }
  54. public void PlayPickupFeedback()
  55. {
  56. if (m_HasPlayedFeedback)
  57. return;
  58. if (PickupSfx)
  59. {
  60. AudioUtility.CreateSFX(PickupSfx, transform.position, AudioUtility.AudioGroups.Pickup, 0f);
  61. }
  62. if (PickupVfxPrefab)
  63. {
  64. var pickupVfxInstance = Instantiate(PickupVfxPrefab, transform.position, Quaternion.identity);
  65. }
  66. m_HasPlayedFeedback = true;
  67. }
  68. }
  69. }