using System.Collections; using System.Collections.Generic; using UnityEngine; using static scr_Models; public class scr_WeaponController : MonoBehaviour { private scr_CharacterController characterController; // creates a reference to our character controller script [Header("Settings")] public WeaponSettingsModel weaponSettings; bool isInitialized; Vector3 newWeaponRotation; Vector3 newWeaponRotationVelocity; Vector3 targetWeaponRotation; Vector3 targetWeaponRotationVelocity; private void Start() { newWeaponRotation = transform.localRotation.eulerAngles; // sets the initial value of newWeaponRotation } // method for setting the characterController on the scr_WeaponController to be the same as the scr_CharacterController on the character public void Initialize(scr_CharacterController CharacterController) { characterController = CharacterController; isInitialized = true; } private void Update() { // if the weapon is not initialized, don't do anything in the update method if (!isInitialized) { return; } targetWeaponRotation.y += weaponSettings.SwayAmount * (weaponSettings.SwayXInverted ? -characterController.input_View.x : characterController.input_View.x) * Time.deltaTime; // calculates left and right float of targetWeaponRotation targetWeaponRotation.x += weaponSettings.SwayAmount * (weaponSettings.SwayYInverted ? characterController.input_View.y : -characterController.input_View.y) * Time.deltaTime; // calculates up and down float of targetWeaponRotation targetWeaponRotation.x = Mathf.Clamp(targetWeaponRotation.x, -weaponSettings.SwayClampX, weaponSettings.SwayClampX); // clamps up and down sway targetWeaponRotation.y = Mathf.Clamp(targetWeaponRotation.y, -weaponSettings.SwayClampY, weaponSettings.SwayClampY); // clamps left and right sway targetWeaponRotation = Vector3.SmoothDamp(targetWeaponRotation, Vector3.zero, ref targetWeaponRotationVelocity, weaponSettings.SwayResetSmoothing); newWeaponRotation = Vector3.SmoothDamp(newWeaponRotation, targetWeaponRotation, ref newWeaponRotationVelocity, weaponSettings.SwaySmoothing); transform.localRotation = Quaternion.Euler(newWeaponRotation); } }