Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- /// Eonir Studio UST
- /// For any informations, questions or ideas feel free to contact me on Discord https://discord.com/invite/wXR2mWY
- /// Thanks for purchasing this asset!
- public class WeaponRecoil : MonoBehaviour
- {
- public float rotationSpeed;
- public float returnRotationSpeed;
- public float positionSpeed;
- public float returnPositionSpeed;
- [Header("Hipfire")]
- public Vector3 RecoilRotation;
- public Vector3 RecoilPosition;
- [Header("Aiming")]
- public Vector3 RecoilRotationAiming;
- public Vector3 RecoilPositionAiming;
- [Header("Empty Fire")]
- public Vector3 RecoilRotationEmptyFire;
- public Vector3 RecoilPositionEmptyFire;
- [Header("State")]
- public bool aiming;
- public bool fireEmpty;
- private Vector3 currentRotation;
- private Vector3 rotation;
- private Vector3 startPosition;
- private Vector3 currentPosition;
- private Transform itemHolder;
- void Start()
- {
- itemHolder = GameObject.FindGameObjectWithTag("ItemHolder").transform;
- startPosition = itemHolder.localPosition;
- }
- private void OnDisable()
- {
- if(itemHolder != null)
- itemHolder.localPosition = startPosition;
- }
- private void FixedUpdate()
- {
- currentRotation = Vector3.Lerp(currentRotation, Vector3.zero, returnRotationSpeed * Time.deltaTime);
- rotation = Vector3.Slerp(rotation, currentRotation, rotationSpeed * Time.fixedDeltaTime);
- itemHolder.localRotation = Quaternion.Euler(rotation);
- currentPosition = Vector3.Lerp(currentPosition, startPosition, returnPositionSpeed * Time.deltaTime);
- itemHolder.localPosition = currentPosition;
- }
- public void Fire()
- {
- if (!fireEmpty)
- {
- if (aiming)
- {
- currentRotation += new Vector3(-RecoilRotationAiming.x, Random.Range(-RecoilRotationAiming.y, RecoilRotationAiming.y), Random.Range(-RecoilRotationAiming.z, RecoilRotationAiming.z));
- currentPosition += new Vector3(Random.Range(-RecoilPositionAiming.x, RecoilPositionAiming.x), Random.Range(-RecoilPositionAiming.y, RecoilPositionAiming.y), Random.Range(-RecoilPositionAiming.z, 0));
- }
- else
- {
- currentRotation += new Vector3(-RecoilRotation.x, Random.Range(-RecoilRotation.y, RecoilRotation.y), Random.Range(-RecoilRotation.z, RecoilRotation.z));
- currentPosition += new Vector3(Random.Range(-RecoilPosition.x, RecoilPosition.x), Random.Range(-RecoilPosition.y, RecoilPosition.y), Random.Range(-RecoilPosition.z, 0));
- }
- }
- else
- {
- currentRotation += new Vector3(-RecoilRotationEmptyFire.x, Random.Range(-RecoilRotationEmptyFire.y, RecoilRotationEmptyFire.y), Random.Range(-RecoilRotationEmptyFire.z, RecoilRotationEmptyFire.z));
- currentPosition += new Vector3(Random.Range(-RecoilPositionEmptyFire.x, RecoilPositionEmptyFire.x), Random.Range(-RecoilPositionEmptyFire.y, RecoilPositionEmptyFire.y), Random.Range(-RecoilPositionEmptyFire.z, 0));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment