/// <summary> /// disables the camera recoil of the shooter and lookdown feature of the weapon /// since these can be nauseating / disorienting /// </summary> protected virtual void UpdateForcedValues() { vp_FPWeaponShooter fpShooter = (CurrentShooter as vp_FPWeaponShooter); if (fpShooter != null) { fpShooter.MotionPositionRecoilCameraFactor = fpShooter.MotionRotationRecoilCameraFactor = 0.0f; } if (CurrentShooter != null) { CurrentShooter.GetFirePosition = VRFirePositionFunc; CurrentShooter.GetFireRotation = VRFireRotationFunc; } if (CurrentFPWeapon != null) { CurrentFPWeapon.LookDownActive = false; if (CurrentFPWeapon.Renderers[0] != null) { CurrentFPWeapon.Renderers[0].shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; } if (ForcedRetraction != 0.0f) { CurrentFPWeapon.RetractionDistance = ForcedRetraction; } } }
/// <summary> /// hooks up the object to the inspector target /// </summary> public virtual void OnEnable() { m_Component = (vp_FPWeaponShooter)target; if (m_Persister == null) m_Persister = new vp_ComponentPersister(); m_Persister.Component = m_Component; m_Persister.IsActive = true; if (m_Component.DefaultState == null) m_Component.RefreshDefaultState(); }
/// <summary> /// restores all the settings on the weapon and shooter from the backup /// made by 'BackupWeaponSettings'. this is to allow for toggling the VR /// feature set on and off at runtime /// </summary> protected virtual void RestoreWeaponSettings(vp_Shooter shooter) { if (shooter == null) { return; } // restore weapon lookdown status vp_FPWeapon fpWeapon = shooter.GetComponent <vp_FPWeapon>(); if (fpWeapon != null) { bool lookDown; if (m_LookDownBackups.TryGetValue(fpWeapon, out lookDown)) { fpWeapon.LookDownActive = lookDown; } float retraction; if (m_RetractionBackups.TryGetValue(fpWeapon, out retraction)) { fpWeapon.RetractionDistance = retraction; } } // restore shooter fire position vp_Shooter.FirePositionFunc pf; if (m_FirePositionFuncBackups.TryGetValue(shooter, out pf)) { shooter.GetFirePosition = pf; } vp_Shooter.FireRotationFunc rf; if (m_FireRotationFuncBackups.TryGetValue(shooter, out rf)) { shooter.GetFireRotation = rf; } // restore shooter camera recoils if (shooter is vp_FPWeaponShooter) { vp_FPWeaponShooter ws = (shooter as vp_FPWeaponShooter); Vector2 recoils; if (m_ShooterCameraRecoilBackups.TryGetValue(ws, out recoils)) { ws.MotionPositionRecoilCameraFactor = recoils.x; ws.MotionRotationRecoilCameraFactor = recoils.y; } } }
/// <summary> /// hooks up the object to the inspector target /// </summary> public virtual void OnEnable() { m_Component = (vp_FPWeaponShooter)target; if (m_Persister == null) { m_Persister = new vp_ComponentPersister(); } m_Persister.Component = m_Component; m_Persister.IsActive = true; if (m_Component.DefaultState == null) { m_Component.RefreshDefaultState(); } }
/// <summary> /// makes a backup of all the settings on the weapon and shooter that this /// component manipulates, for later restoration in 'RestoreWeaponSettings' /// </summary> protected virtual void BackupWeaponSettings() { if (WeaponHandler == null) { return; } foreach (vp_Weapon weapon in WeaponHandler.Weapons) { if (weapon == null) { continue; } // backup weapon lookdown status if (weapon is vp_FPWeapon) { vp_FPWeapon fpWeapon = (weapon as vp_FPWeapon); if (!m_LookDownBackups.ContainsKey(fpWeapon)) { m_LookDownBackups.Add(fpWeapon, fpWeapon.LookDownActive); } if (!m_RetractionBackups.ContainsKey(fpWeapon)) { m_RetractionBackups.Add(fpWeapon, fpWeapon.RetractionDistance); } } // backup shooter fire position #if UNITY_5_3_OR_NEWER vp_Shooter shooter = weapon.GetComponentInChildren <vp_Shooter>(true); // IMPORTANT: this works in Unity 5.3.4f1, but is not guaranteed to work in earlier versions #else vp_Shooter shooter = weapon.GetComponentInChildren <vp_Shooter>(); #endif if (shooter == null) { continue; } if ((!(shooter.GetFirePosition == null)) && (!m_FirePositionFuncBackups.ContainsKey(shooter))) { m_FirePositionFuncBackups.Add(shooter, shooter.GetFirePosition); } if ((!(shooter.GetFireRotation == null)) && (!m_FireRotationFuncBackups.ContainsKey(shooter))) { m_FireRotationFuncBackups.Add(shooter, shooter.GetFireRotation); } // backup if (shooter is vp_FPWeaponShooter) { vp_FPWeaponShooter ws = (shooter as vp_FPWeaponShooter); if (!m_ShooterCameraRecoilBackups.ContainsKey(ws)) { m_ShooterCameraRecoilBackups.Add(ws, new Vector2(ws.MotionPositionRecoilCameraFactor, ws.MotionRotationRecoilCameraFactor)); } } Renderer r = weapon.GetComponentInChildren <Renderer>(); if (r != null) { r.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; } } }
/// <summary> /// sets up simplified 3rd person weapon child objects based on the /// current first person weapons under the vp_FPCamera component /// </summary> public static void ConvertWeaponsTo3rdPerson(GameObject player) { int weaponsConverted = 0; Transform oldWeaponGroup = null; // typically the 'FPSCamera' object GameObject newWeaponGroup = new GameObject("Weapons"); foreach (vp_FPWeapon fpWeapon in player.transform.GetComponentsInChildren <vp_FPWeapon>()) { // detect old weapons parent and put the new one alongside it in hierarchy if (oldWeaponGroup == null) { oldWeaponGroup = fpWeapon.transform.parent; newWeaponGroup.transform.parent = oldWeaponGroup.parent; newWeaponGroup.transform.localPosition = Vector3.zero; newWeaponGroup.transform.localEulerAngles = Vector3.zero; newWeaponGroup.transform.localScale = Vector3.one; } // create a new gameobject for each weapon. this way we get rid of // any custom components that may be hard for the editor code to // delete because of component interdependencies GameObject newWeaponGO = new GameObject(fpWeapon.gameObject.name); newWeaponGO.transform.parent = newWeaponGroup.transform; // NOTE: position of this transform is irrelevant because a 3rd // person vp_Weapon is just an invisible, locigal entity, represented // by its '3rdPersonWeapon' gameobject reference in the 3d world. // still, let's place the weapons in a fairly neutral position for // clarity: say, the center of the player facing forwards newWeaponGO.transform.localPosition = Vector3.up; newWeaponGO.transform.localEulerAngles = Vector3.zero; newWeaponGO.transform.localScale = Vector3.one; // convert vp_FPWeapon component into a vp_Weapon component on the new gameobject vp_Weapon tdpWeapon = newWeaponGO.AddComponent <vp_Weapon>(); vp_EditorUtility.CopyValuesFromDerivedComponent(fpWeapon, tdpWeapon, true, true, null); if (m_CopyStates && !string.IsNullOrEmpty(m_StatePath)) { vp_EditorUtility.GenerateStatesAndPresetsFromDerivedComponent(fpWeapon, tdpWeapon, m_StatePath); } // FP shooters (used for projectile weapons) vp_FPWeaponShooter fpShooter = fpWeapon.GetComponent <vp_FPWeaponShooter>(); if (fpShooter != null) { vp_WeaponShooter tdpFpShooter = newWeaponGO.AddComponent <vp_WeaponShooter>(); vp_EditorUtility.CopyValuesFromDerivedComponent(fpShooter, tdpFpShooter, true, true, null); if (m_CopyStates && !string.IsNullOrEmpty(m_StatePath)) { vp_EditorUtility.GenerateStatesAndPresetsFromDerivedComponent(fpShooter, tdpFpShooter, m_StatePath); } } // regular shooters (used for melee weapons) vp_WeaponShooter shooter = fpWeapon.GetComponent <vp_WeaponShooter>(); if ((shooter != null) && !(shooter is vp_FPWeaponShooter)) { vp_WeaponShooter tdpShooter = newWeaponGO.AddComponent <vp_WeaponShooter>(); vp_EditorUtility.CopyValuesFromDerivedComponent(shooter, tdpShooter, true, true, null); if (m_CopyStates && !string.IsNullOrEmpty(m_StatePath)) { vp_EditorUtility.GenerateStatesAndPresetsFromDerivedComponent(shooter, tdpShooter, m_StatePath); } } // reloaders vp_FPWeaponReloader fpReloader = fpWeapon.GetComponent <vp_FPWeaponReloader>(); if (fpReloader != null) { vp_WeaponReloader tdpReloader = newWeaponGO.AddComponent <vp_WeaponReloader>(); vp_EditorUtility.CopyValuesFromDerivedComponent(fpReloader, tdpReloader, true, true, null); // reloader is no vp_Component so don't generate states & presets } // copy weapon thrower vp_FPWeaponThrower fpThrower = fpWeapon.GetComponent <vp_FPWeaponThrower>(); if (fpThrower != null) { vp_WeaponThrower tdpThrower = newWeaponGO.AddComponent <vp_WeaponThrower>(); vp_EditorUtility.CopyValuesFromDerivedComponent(fpWeapon, tdpThrower, true, true, null); } // copy item identifier vp_ItemIdentifier identifier = fpWeapon.GetComponent <vp_ItemIdentifier>(); if (identifier != null) { vp_ItemIdentifier newIdentifier = newWeaponGO.AddComponent <vp_ItemIdentifier>(); newIdentifier.Type = identifier.Type; } weaponsConverted++; } // delete the old weapon group (FPSCamera object) if (oldWeaponGroup != null) { GameObject.DestroyImmediate(oldWeaponGroup.gameObject); } EditorUtility.DisplayDialog( weaponsConverted + " weapons converted", (weaponsConverted > 0 ? ( ((m_CopyStates && !string.IsNullOrEmpty(m_StatePath)) ? ("Any states with compatible values have been retained and their presets can be found in the folder:\n\t\t'" + m_StatePath + "'") : "") ) : ""), "OK"); }