Hi this is a little hard to explain and my video caputure is not working so i will post photos
This is my correct idle state
This is mid-sprint animation
When I switch weapons (disabling one and enabling the other) mid-animation the other is fine
However when I switch back to my other its locked in its previous known position and does not reset even through setting the transform.position and eulerAngles to default through code. Switching weapons again does not help either
using Cinemachine;
using StarterAssets;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
///
/// HANDLES ///
///
// Objects _g
[SerializeField] GameObject _gBulletImpact, _gCleanupContainer;
[SerializeField] ParticleSystem _gMuzzleFlashVFX, _gBulletTracer;
// External Components _e
[SerializeField] Camera _eCam;
//
[SerializeField] CinemachineVirtualCamera _eCMvCam;
//
[SerializeField] FirstPersonController _eFirstPersonController;
//
[SerializeField] StarterAssetsInputs _eInputs;
//
[SerializeField] Player _ePlayer;
// Internal Components _n
Animator _nAnimator;
// User Interface _u
// Vectors _v
Vector3 _vDefaultPosition, _vDefaultRotation;
// Lists _l, _a, _d, _q
// Variables _f, _i, _b, _s
[SerializeField] float _fFireRate, _fReloadTime, _fZoomedFOV;
float _fRange, _fNextFire, _fNextReload, _fReloadCooldown, _fZoomedSensitivity;
float _fDefaultFOV = 66f;
float _fDefaultSensitivity = 2f;
//
[SerializeField] int _iDamageOutput, _iMagazineSize, _iAmmoCount, _iAmmoType, _iStartingMagCount, _iIndexValue;
public int MagCount { get; set; }
public int MagazineSize => _iMagazineSize;
public int AmmoCount => _iAmmoCount;
//
bool _bCanFire, _bReloading;
public bool IsOwned { get; set; }
public bool Reloading => _bReloading;
public int IndexValue => _iIndexValue;
///
/// ASSIGN ///
///
void AssignComponents()
{
_nAnimator = GetComponent<Animator>();
}
void AssignValues()
{
_vDefaultPosition = new Vector3(.332f, -.192f, .704f);
_vDefaultRotation = new Vector3(0, -90.66f, -1);
_fRange = _eCam.farClipPlane;
_fNextFire = -1f;
_fNextReload = -1f;
_fReloadCooldown = _fReloadTime / 2;
_fZoomedSensitivity = _fZoomedFOV / _fDefaultFOV;
if (_iAmmoCount <= 0) { _bCanFire = false; }
else { _bCanFire = true; }
IsOwned = false;
}
///
/// INITIALIZATION & EXECUTION ///
///
void Awake()
{
}
void OnEnable()
{
AssignComponents();
AssignValues();
EnableActiveWeapon();
IsOwned = true;
}
// THREAD START //
void EnableActiveWeapon() // Communicates active weapon info with other components
{
SwitchAmmoType(); // Communicates with IM to update ammo amount for the active weapon's ammo type
void SwitchAmmoType()
{
switch (_iAmmoType)
{
case 0: // Carbine
MagCount = InventoryManager.Instance.iCarbineMags;
break;
case 1: // Pistol
MagCount = InventoryManager.Instance.iPistolMags;
break;
}
}
RelayCurrentWeaponInfo(); // Relays current weapon to Player -> IM, IM updates UI / Assigns the current weapon to SAI animator component
void RelayCurrentWeaponInfo()
{
_ePlayer.GetCurrentWeapon(gameObject);
InventoryManager.Instance.GetActiveWeapon();
InventoryManager.Instance.UpdateAmmoText();
_eInputs.eWeaponAnimator = GetComponent<Animator>();
}
}
// THREAD END //
// THREAD START //
void Update() // On update
{
CheckInput(); // Check for weapon input if not reloading
void CheckInput()
{
if (_bReloading)
{
_eInputs.sprint = false;
}
else
{
FireInput(); // Fire weapon if ButtonDown, cooldown & _bCanFire is met, & not sprinting
void FireInput()
{
if (Input.GetMouseButtonDown(0) && (Time.time > _fNextFire) && _bCanFire && !_eInputs.sprint)
{
_fNextFire = Time.time + _fFireRate;
FireWeapon(); // Process hit, play feedback, & update ammo count
void FireWeapon()
{
ProcessHit(); // Raycast target, if enemy was struck damage it, else instantiate impactVFX at hit location (if any)
void ProcessHit()
{
RaycastHit hitInfo;
bool hit = Physics.Raycast(_eCam.transform.position, _eCam.transform.forward, out hitInfo, _fRange);
if (hit)
{
if (hitInfo.transform.CompareTag("Enemy"))
{
Enemy enemy = hitInfo.transform.GetComponent<Enemy>();
enemy.EnemyDamage(_iDamageOutput);
}
else
{
GameObject bulletImpact = Instantiate(_gBulletImpact, hitInfo.point, Quaternion.LookRotation(hitInfo.normal));
bulletImpact.transform.parent = _gCleanupContainer.transform;
}
}
else { return; }
}
PlayFeedback(); // Set recoil trigger, play muzzleFX & bullet tracer
void PlayFeedback()
{
_nAnimator.SetTrigger("WeaponFired");
_gMuzzleFlashVFX.Play();
_gBulletTracer.Play();
}
UpdateAmmoCount(); // Subtract ammo & update IM -> UI if mag is not empty, else set _bCanFire false
void UpdateAmmoCount()
{
--_iAmmoCount;
InventoryManager.Instance.UpdateAmmoText();
if (_iAmmoCount <= 0)
{
_bCanFire = false;
}
}
}
}
}
ReloadInput(); // Reload weapon if button down, cooldown is met, not sprinting or reloading, more than 0 mags in inventory, & ammo in mag is not full
void ReloadInput()
{
if (Input.GetKeyDown(KeyCode.R) && (Time.time > _fNextReload) && !_eInputs.sprint && !_bReloading &&
(MagCount > 0) && (_iAmmoCount < _iMagazineSize))
{
_fNextReload = Time.time + _fReloadCooldown;
StartCoroutine(Reload()); // Initiate reload coroutine
IEnumerator Reload()
{
InitiateReload(); // Disable weapon input, subtract magazine, & set animator "Reload" trigger
void InitiateReload()
{
_bReloading = true;
_bCanFire = false;
_nAnimator.SetTrigger("Reload");
switch (_iAmmoType)
{
case 0: // Pistol
--InventoryManager.Instance.iPistolMags;
MagCount = InventoryManager.Instance.iPistolMags;
break;
case 1: // Carbine
--InventoryManager.Instance.iCarbineMags;
MagCount = InventoryManager.Instance.iCarbineMags;
break;
}
}
yield return new WaitForSeconds(_fReloadTime);
FinalizeReload(); // Resupply ammo, update UI, & re-enable weapon input
void FinalizeReload()
{
_iAmmoCount = _iMagazineSize;
InventoryManager.Instance.UpdateAmmoText();
_bCanFire = true;
_bReloading = false;
}
}
}
}
ZoomInput(); // Zoom if button held & not sprinting, then adjust sensitivity
void ZoomInput()
{
if (Input.GetMouseButton(1) && !_eInputs.sprint)
{
_eCMvCam.m_Lens.FieldOfView = _fZoomedFOV;
_eFirstPersonController.RotationSpeed = _fZoomedSensitivity;
}
else
{
_eCMvCam.m_Lens.FieldOfView = _fDefaultFOV;
_eFirstPersonController.RotationSpeed = _fDefaultSensitivity;
}
}
}
}
}
// THREAD END //
// Misc
public void UpdateMagCount()
{
switch (_iAmmoType)
{
case 0: // Pistol
MagCount = InventoryManager.Instance.iPistolMags;
break;
case 1: // Carbine
MagCount = InventoryManager.Instance.iCarbineMags;
break;
}
}
}
and my modification to the StandardAssetsInput script (lines 28-70)
using Unity.VisualScripting.Dependencies.NCalc;
using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
using static UnityEngine.Rendering.DebugUI;
#endif
namespace StarterAssets
{
public class StarterAssetsInputs : MonoBehaviour
{
public Animator eWeaponAnimator;
[Header("Character Input Values")]
public Vector2 move;
public Vector2 look;
public bool jump;
public bool sprint;
[Header("Movement Settings")]
public bool analogMovement;
[Header("Mouse Cursor Settings")]
public bool cursorLocked = true;
public bool cursorInputForLook = true;
#if ENABLE_INPUT_SYSTEM
void Start()
{
eWeaponAnimator = InventoryManager.Instance.ActiveWeapon.GetComponent<Animator>();
}
void Update()
{
if (move != Vector2.zero)
{
if (sprint)
{
SprintState();
}
else
{
WalkState();
}
}
else if (move == Vector2.zero)
{
IdleState();
}
}
void IdleState()
{
eWeaponAnimator.SetBool("Sprinting", false);
eWeaponAnimator.SetBool("Walking", false);
eWeaponAnimator.SetBool("Idle", true);
}
void WalkState()
{
eWeaponAnimator.SetBool("Sprinting", false);
eWeaponAnimator.SetBool("Walking", true);
eWeaponAnimator.SetBool("Idle", false);
}
void SprintState()
{
eWeaponAnimator.SetBool("Sprinting", true);
eWeaponAnimator.SetBool("Walking", false);
eWeaponAnimator.SetBool("Idle", false);
}
public void OnMove(InputValue value)
{
MoveInput(value.Get<Vector2>());
}
public void OnLook(InputValue value)
{
if(cursorInputForLook)
{
LookInput(value.Get<Vector2>());
}
}
public void OnJump(InputValue value)
{
JumpInput(value.isPressed);
}
public void OnSprint(InputValue value)
{
SprintInput(value.isPressed);
}
#endif
public void MoveInput(Vector2 newMoveDirection)
{
move = newMoveDirection;
}
public void LookInput(Vector2 newLookDirection)
{
look = newLookDirection;
}
public void JumpInput(bool newJumpState)
{
jump = newJumpState;
}
public void SprintInput(bool newSprintState)
{
sprint = newSprintState;
}
private void OnApplicationFocus(bool hasFocus)
{
SetCursorState(cursorLocked);
}
private void SetCursorState(bool newState)
{
Cursor.lockState = newState ? CursorLockMode.Locked : CursorLockMode.None;
}
}
}
sorry if this is hard to follow, thanks in advance for any help
EDIT: InventoryController script which handles the switching of weapons (56-91)
using StarterAssets;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
using UnityEngine;
public class InventoryManager : MonoBehaviour
{
static InventoryManager instance;
public static InventoryManager Instance => instance;
///
/// HANDLES ///
///
// Objects _g
[SerializeField] GameObject _gPistol, _gCarbine, _gWeaponContainer;
// External Components _e
StarterAssetsInputs _eInputs;
//
Player _ePlayer;
//
public Weapon ActiveWeapon { get; set; }
// Internal Components _n
// User Interface _u
// Vectors _v
// Lists _l, _a, _d, _q
[SerializeField] List<GameObject> _lWeaponsList;
// Variables _f, _i, _b, _s
public int iCarbineMags;
public int iPistolMags;
int _iCurrentIndex;
float _fNextSwitch, _fSwitchCooldown;
///
/// ASSIGN ///
///
void AssignComponents()
{
_ePlayer = GetComponent<Player>();
_eInputs = _ePlayer.gameObject.GetComponent<StarterAssetsInputs>();
}
void AssignValues()
{
_fNextSwitch = -1f;
_fSwitchCooldown = .5f;
}
///
/// INITIALIZATION & EXECUTION ///
///
void Awake()
{
instance = this;
AssignComponents();
}
void Update()
{
AlphaInput();
void AlphaInput()
{
if (Input.GetKeyDown(KeyCode.Alpha1) && (Time.time > _fNextSwitch))
{
_fNextSwitch = Time.time + _fSwitchCooldown;
RequestWeapon(0);
}
if (Input.GetKeyDown(KeyCode.Alpha2) && (Time.time > _fNextSwitch))
{
_fNextSwitch = Time.time + _fSwitchCooldown;
RequestWeapon(1);
}
}
}
// Misc
void RequestWeapon(int requestedIndex)
{
Weapon requestedWeapon = _lWeaponsList[requestedIndex].GetComponent<Weapon>();
if (requestedWeapon.IsOwned && !ActiveWeapon.Reloading && !_eInputs.sprint && (_iCurrentIndex != requestedIndex))
{
SetActiveWeapon(requestedIndex);
}
}
void SetActiveWeapon(int requestedIndex)
{
ActiveWeapon.gameObject.SetActive(false);
_lWeaponsList[requestedIndex].gameObject.SetActive(true);
_iCurrentIndex = requestedIndex;
ActiveWeapon.UpdateMagCount();
UpdateAmmoText();
}
public void GetActiveWeapon()
{
ActiveWeapon = _ePlayer.ActiveWeapon.GetComponent<Weapon>();
_iCurrentIndex = ActiveWeapon.IndexValue;
}
void OnTriggerEnter(Collider other)
{
switch (other.tag)
{
case "PistolPickup":
iPistolMags++;
SetActiveWeapon(0);
break;
case "CarbinePickup":
iCarbineMags++;
SetActiveWeapon(1);
break;
case "PistolMag":
iPistolMags++;
ActiveWeapon.UpdateMagCount();
UpdateAmmoText();
break;
case "CarbineMag":
iCarbineMags++;
ActiveWeapon.UpdateMagCount();
UpdateAmmoText();
break;
}
Destroy(other.gameObject);
}
public void UpdateAmmoText()
{
UIManager.Instance.UpdateAmmoTextToUI(ActiveWeapon.AmmoCount, ActiveWeapon.MagazineSize, ActiveWeapon.MagCount);
}
}
really sorry if this is a lot of script dumping im just at a loss. thanks again!



