Eonirr

Untitled

Dec 26th, 2025
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. /// Eonir Studio UST
  4. /// For any informations, questions or ideas feel free to contact me on Discord https://discord.com/invite/wXR2mWY
  5. /// Thanks for purchasing this asset!
  6.  
  7.  
  8. public class WeaponRecoil : MonoBehaviour
  9. {
  10. public float rotationSpeed;
  11. public float returnRotationSpeed;
  12. public float positionSpeed;
  13. public float returnPositionSpeed;
  14.  
  15. [Header("Hipfire")]
  16. public Vector3 RecoilRotation;
  17. public Vector3 RecoilPosition;
  18.  
  19. [Header("Aiming")]
  20. public Vector3 RecoilRotationAiming;
  21. public Vector3 RecoilPositionAiming;
  22.  
  23. [Header("Empty Fire")]
  24. public Vector3 RecoilRotationEmptyFire;
  25. public Vector3 RecoilPositionEmptyFire;
  26.  
  27. [Header("State")]
  28. public bool aiming;
  29. public bool fireEmpty;
  30.  
  31. private Vector3 currentRotation;
  32. private Vector3 rotation;
  33.  
  34. private Vector3 startPosition;
  35. private Vector3 currentPosition;
  36.  
  37. private Transform itemHolder;
  38.  
  39. void Start()
  40. {
  41. itemHolder = GameObject.FindGameObjectWithTag("ItemHolder").transform;
  42. startPosition = itemHolder.localPosition;
  43. }
  44.  
  45.  
  46. private void OnDisable()
  47. {
  48. if(itemHolder != null)
  49. itemHolder.localPosition = startPosition;
  50. }
  51.  
  52. private void FixedUpdate()
  53. {
  54. currentRotation = Vector3.Lerp(currentRotation, Vector3.zero, returnRotationSpeed * Time.deltaTime);
  55. rotation = Vector3.Slerp(rotation, currentRotation, rotationSpeed * Time.fixedDeltaTime);
  56. itemHolder.localRotation = Quaternion.Euler(rotation);
  57.  
  58. currentPosition = Vector3.Lerp(currentPosition, startPosition, returnPositionSpeed * Time.deltaTime);
  59. itemHolder.localPosition = currentPosition;
  60. }
  61.  
  62. public void Fire()
  63. {
  64. if (!fireEmpty)
  65. {
  66. if (aiming)
  67. {
  68. currentRotation += new Vector3(-RecoilRotationAiming.x, Random.Range(-RecoilRotationAiming.y, RecoilRotationAiming.y), Random.Range(-RecoilRotationAiming.z, RecoilRotationAiming.z));
  69. currentPosition += new Vector3(Random.Range(-RecoilPositionAiming.x, RecoilPositionAiming.x), Random.Range(-RecoilPositionAiming.y, RecoilPositionAiming.y), Random.Range(-RecoilPositionAiming.z, 0));
  70. }
  71. else
  72. {
  73. currentRotation += new Vector3(-RecoilRotation.x, Random.Range(-RecoilRotation.y, RecoilRotation.y), Random.Range(-RecoilRotation.z, RecoilRotation.z));
  74. currentPosition += new Vector3(Random.Range(-RecoilPosition.x, RecoilPosition.x), Random.Range(-RecoilPosition.y, RecoilPosition.y), Random.Range(-RecoilPosition.z, 0));
  75. }
  76. }
  77. else
  78. {
  79. currentRotation += new Vector3(-RecoilRotationEmptyFire.x, Random.Range(-RecoilRotationEmptyFire.y, RecoilRotationEmptyFire.y), Random.Range(-RecoilRotationEmptyFire.z, RecoilRotationEmptyFire.z));
  80. currentPosition += new Vector3(Random.Range(-RecoilPositionEmptyFire.x, RecoilPositionEmptyFire.x), Random.Range(-RecoilPositionEmptyFire.y, RecoilPositionEmptyFire.y), Random.Range(-RecoilPositionEmptyFire.z, 0));
  81. }
  82. }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment