/// <summary> /// this callback is triggered when the activity in question /// deactivates /// </summary> protected virtual void OnStop_Attack() { // the Attack activity does not automatically disable the // component's Attack state, so schedule disabling it in // 'AttackStateDisableDelay' seconds vp_Timer.In(AttackStateDisableDelay, delegate() { if (!m_Player.Attack.Active) { if (m_CurrentWeapon != null) { m_CurrentWeapon.SetState("Attack", false); } } }, m_DisableAttackStateTimer); }
/// <summary> /// simulates a melee swing by carrying out a sequence of /// timers, state changes and soft forces on the weapon springs /// </summary> protected void UpdateAttack() { if (!Player.Attack.Active) { return; } if (Player.SetWeapon.Active) { return; } if (m_Weapon == null) { return; } if (!m_Weapon.Wielded) { return; } if (Time.time < m_NextAllowedSwingTime) { return; } m_NextAllowedSwingTime = Time.time + SwingRate; // enable random attack states on the melee and weapon components if (AttackPickRandomState) { PickAttack(); } // set 'raise' state (of the chosen attack state) on the weapon component m_Weapon.SetState(WeaponStatePull); m_Weapon.Refresh(); // after a short delay, swing the weapon vp_Timer.In(SwingDelay, delegate() { // play a random swing sound if (SoundSwing.Count > 0) { Audio.pitch = Random.Range(SoundSwingPitch.x, SoundSwingPitch.y) * Time.timeScale; Audio.clip = (AudioClip)SoundSwing[(int)Random.Range(0, (SoundSwing.Count))]; Audio.Play(); } // switch to the swing state m_Weapon.SetState(WeaponStatePull, false); m_Weapon.SetState(WeaponStateSwing); m_Weapon.Refresh(); // apply soft forces of the current attack m_Weapon.AddSoftForce(SwingPositionSoftForce, SwingRotationSoftForce, SwingSoftForceFrames); // check for target impact after a predetermined duration vp_Timer.In(ImpactTime, delegate() { // perform a sphere cast ray from center of controller, at height // of camera and along camera angle RaycastHit hit; Ray ray = new Ray(new Vector3(m_Controller.Transform.position.x, m_Camera.Transform.position.y, m_Controller.Transform.position.z), m_Camera.Transform.forward); Physics.SphereCast(ray, DamageRadius, out hit, DamageRange, vp_Layer.Mask.BulletBlockers); // hit something: perform impact functionality if (hit.collider != null) { SpawnImpactFX(hit); ApplyDamage(hit); ApplyRecoil(); #if (UNITY_EDITOR) // draw debug info if applicable if (DrawDebugObjects) { DebugDrawImpact(hit); } #endif } else { // didn't hit anything: carry on swinging until time is up vp_Timer.In(SwingDuration - ImpactTime, delegate() { m_Weapon.StopSprings(); Reset(); }, SwingDurationTimer); } }, ImpactTimer); }, SwingDelayTimer); }