/////////////////////////////////////////////////////////// // moves the current weapon model to its exit offset, changes // the weapon model and moves the new weapon into view. // an optional callback may be provided to execute a method // as the new weapon becomes active /////////////////////////////////////////////////////////// public void SwitchWeapon(int weapon, WeaponSwitchDelegate callback = null) { // prevent firing while putting the current weapon away if (CurrentShooter != null) { CurrentShooter.PreventFiring(0.5f); } if (CurrentWeapon != null) { CurrentWeapon.StateManager.Reset(); // rotate and move the current weapon out of view CurrentWeapon.PositionOffset = CurrentWeapon.PositionExitOffset; CurrentWeapon.RotationOffset = CurrentWeapon.RotationExitOffset; CurrentWeapon.Refresh(); // play unwield sound if (CurrentWeapon.audio != null) { if (CurrentWeapon.SoundUnWield != null) { CurrentWeapon.audio.pitch = 1; CurrentWeapon.audio.PlayOneShot(CurrentWeapon.SoundUnWield); } } } // cancel any already ongoing weapon switching activity CancelWeaponSwitch(); // create a new event to switch the weapon once model has had // time enough to rotate out of view. 0.15 seconds should do. m_SwitchWeaponTimer = vp_Timer.In(0.15f, delegate() { // switch weapon SetWeapon(weapon); // prevent firing while taking out the new weapon if (CurrentShooter != null) { CurrentShooter.PreventFiring(0.5f); } // force the 'rotated down' angle and position onto the new // weapon when it spawns, or it will pop into view if (CurrentWeapon != null) { CurrentWeapon.RotationOffset = CurrentWeapon.RotationExitOffset; CurrentWeapon.PositionOffset = CurrentWeapon.PositionExitOffset; CurrentWeapon.SnapSprings(); CurrentWeapon.SnapPivot(); CurrentWeapon.SnapZoom(); CurrentWeapon.Refresh(); } // create an event to show the new weapon in 0.15 seconds m_ShowWeaponTimer = vp_Timer.In(0.15f, delegate() { if (CurrentWeapon != null) { // we do this by smoothly restoring the loaded weapon's // desired position and angle CurrentWeapon.PositionOffset = CurrentWeapon.DefaultPosition; CurrentWeapon.RotationOffset = CurrentWeapon.DefaultRotation; CurrentWeapon.Refresh(); // play wield sound if (CurrentWeapon.audio != null) { if (CurrentWeapon.SoundWield != null) { CurrentWeapon.audio.pitch = 1; CurrentWeapon.audio.PlayOneShot(CurrentWeapon.SoundWield); } } } // execute user defined callback, if provided if (callback != null) { callback(); } }); }); }