Beg_Productions

Full Code to make a recoil system, again

Sep 28th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.11 KB | None | 0 0
  1. //Again uploaded, yes, because i lost the other page i uploaded as guest, i'm using my older name :( of twitter.
  2. using UnityEngine;
  3. using System.Collections;
  4. using MoreMountains.Tools;
  5.  
  6. namespace MoreMountains.CorgiEngine
  7. {  
  8.     /// <summary>
  9.     /// Add this class to a character so it can use weapons
  10.     /// Note that this component will trigger animations (if their parameter is present in the Animator), based on
  11.     /// the current weapon's Animations
  12.     /// Animator parameters : defined from the Weapon's inspector
  13.     /// </summary>
  14.     [AddComponentMenu("Corgi Engine/Character/Abilities/Character Handle Weapon")]
  15.     public class CharacterHandleWeapon : CharacterAbility
  16.     {
  17.         //Recoil, pls work :(     (when you press the A botton the is in use the +1 recoil for teleport the player +1, and when the D botton is pressed is in use the recoilRight for teleport the player -1, according with the animations of the character)
  18.         public Vector3 recoilLeft = Vector3.zero;
  19.         public Vector3 recoilRight = Vector3.zero;
  20.         public Vector3 recoilRight2 = Vector3.zero;
  21.  
  22.         //Drag and drop the world collider here
  23.         public BoxCollider2D worldCollider;
  24.  
  25.         void Update()
  26.         {
  27.  
  28.             if (Input.GetKeyDown(KeyCode.A))
  29.             {
  30.                 recoilRight = recoilLeft;
  31.                 Debug.Log("'A' button");
  32.                 return;
  33.             }
  34.  
  35.             if (Input.GetKeyDown(KeyCode.D))
  36.             {
  37.                 recoilRight = recoilRight2;
  38.                 Debug.Log("'D' button");
  39.                 return;
  40.             }
  41.  
  42.             if (Input.GetKeyDown(KeyCode.W))
  43.             {
  44.                 Debug.Log("'W' button");
  45.                 return;
  46.             }
  47.  
  48.  
  49.         }
  50.  
  51.         /// This method is only used to display a helpbox text at the beginning of the ability's inspector
  52.         public override string HelpBoxText() { return "This component will allow your character to pickup and use weapons. What the weapon will do is defined in the Weapon classes. This just describes the behaviour of the 'hand' holding the weapon, not the weapon itself. Here you can set an initial weapon for your character to start with, allow weapon pickup, and specify a weapon attachment (a transform inside of your character, could be just an empty child gameobject, or a subpart of your model."; }
  53.  
  54.         /// the initial weapon owned by the character
  55.         public Weapon InitialWeapon;   
  56.         /// the position the weapon will be attached to. If left blank, will be this.transform.
  57.         public Transform WeaponAttachment;
  58.  
  59.         /// if this is set to true, the character can pick up PickableWeapons
  60.         public bool CanPickupWeapons = true;
  61.         /// returns the current equipped weapon
  62.         [ReadOnly]
  63.         public Weapon CurrentWeapon;
  64.         /// if this is true you won't have to release your fire button to auto reload
  65.         public bool ContinuousPress = false;
  66.         /// if this is true this animator will be automatically bound to the weapon
  67.         public bool AutomaticallyBindAnimator = true;
  68.         /// whether or not this character getting hit should interrupt its attack (will only work if the weapon is marked as interruptable)
  69.         public bool GettingHitInterruptsAttack = false;
  70.  
  71.         public Animator CharacterAnimator { get; set; }
  72.  
  73.         protected float _fireTimer = 0f;
  74.         protected float _secondaryHorizontalMovement;
  75.         protected float _secondaryVerticalMovement;
  76.         protected WeaponAim _aimableWeapon;
  77.         protected WeaponIK _weaponIK;
  78.         protected Transform _leftHandTarget = null;
  79.         protected Transform _rightHandTarget = null;
  80.  
  81.         // Initialization
  82.         protected override void Initialization ()
  83.         {
  84.             base.Initialization();
  85.                                
  86.             Setup ();
  87.         }
  88.  
  89.  
  90.  
  91.         /// <summary>
  92.         /// Grabs various components and inits stuff
  93.         /// </summary>
  94.         public virtual void Setup()
  95.         {
  96.             // filler if the WeaponAttachment has not been set
  97.             if (WeaponAttachment==null)
  98.             {
  99.                 WeaponAttachment=transform;
  100.             }      
  101.             if (_animator != null)
  102.             {
  103.                 _weaponIK = _animator.GetComponent<WeaponIK> ();
  104.             }  
  105.             // we set the initial weapon
  106.             if (InitialWeapon != null)
  107.             {
  108.                 ChangeWeapon(InitialWeapon, null);         
  109.             }
  110.             _character = gameObject.GetComponentNoAlloc<Character> ();
  111.             CharacterAnimator = _animator;
  112.         }
  113.  
  114.         /// <summary>
  115.         /// Every frame we check if it's needed to update the ammo display
  116.         /// </summary>
  117.         public override void ProcessAbility()
  118.         {
  119.             base.ProcessAbility ();
  120.             UpdateAmmoDisplay ();
  121.         }
  122.  
  123.         /// <summary>
  124.         /// Gets input and triggers methods based on what's been pressed
  125.         /// </summary>
  126.         protected override void HandleInput ()
  127.         {          
  128.  
  129.             if ((_inputManager.ShootButton.State.CurrentState == MMInput.ButtonStates.ButtonDown) || (_inputManager.ShootAxis == MMInput.ButtonStates.ButtonDown))
  130.             {
  131.                 ShootStart();
  132.                
  133.             }
  134.  
  135.             if (CurrentWeapon != null)
  136.             {
  137.                 if (ContinuousPress && (CurrentWeapon.TriggerMode == Weapon.TriggerModes.Auto) && (_inputManager.ShootButton.State.CurrentState == MMInput.ButtonStates.ButtonPressed))
  138.                 {
  139.                     ShootStart();
  140.                 }
  141.                 if (ContinuousPress && (CurrentWeapon.TriggerMode == Weapon.TriggerModes.Auto) && (_inputManager.ShootAxis == MMInput.ButtonStates.ButtonPressed))
  142.                 {
  143.                     ShootStart();
  144.                 }
  145.             }          
  146.  
  147.             if (_inputManager.ReloadButton.State.CurrentState == MMInput.ButtonStates.ButtonDown)
  148.             {
  149.                 Reload();
  150.             }
  151.  
  152.             if ((_inputManager.ShootButton.State.CurrentState == MMInput.ButtonStates.ButtonUp) || (_inputManager.ShootAxis == MMInput.ButtonStates.ButtonUp))
  153.             {
  154.                 ShootStop();
  155.             }
  156.  
  157.             if (CurrentWeapon != null)
  158.             {
  159.                 if ((CurrentWeapon.WeaponState.CurrentState == Weapon.WeaponStates.WeaponDelayBetweenUses)
  160.                 && ((_inputManager.ShootAxis == MMInput.ButtonStates.Off) && (_inputManager.ShootButton.State.CurrentState == MMInput.ButtonStates.Off)))
  161.                 {
  162.                     CurrentWeapon.WeaponInputStop();
  163.                 }
  164.             }            
  165.         }
  166.  
  167.         /// <summary>
  168.         /// Causes the character to start shooting
  169.         /// </summary>
  170.         public virtual void ShootStart()
  171.         {
  172.             // if the Shoot action is enabled in the permissions, we continue, if not we do nothing.  If the player is dead we do nothing.
  173.             if (!AbilityPermitted
  174.             || (CurrentWeapon == null)
  175.             || (_condition.CurrentState != CharacterStates.CharacterConditions.Normal)
  176.             || (_movement.CurrentState == CharacterStates.MovementStates.LadderClimbing))
  177.             {
  178.  
  179.                 return;
  180.             }
  181.  
  182.             //This is where the player will be after the recoil.
  183.             Vector3 positionAfterRecoil = transform.position + recoilRight;
  184.  
  185.             //If we are outside the left side of the box, snap to the left side of the box
  186.             if (positionAfterRecoil.x < worldCollider.bounds.min.x)
  187.             {
  188.                 positionAfterRecoil.x = worldCollider.bounds.min.x;
  189.             }
  190.             //if we are outside the right side of the box, snap to the right side of the box
  191.             else if (positionAfterRecoil.x > worldCollider.bounds.max.x)
  192.             {
  193.                 positionAfterRecoil.x = worldCollider.bounds.max.x;
  194.             }
  195.            
  196.  
  197.             //This is for teleport the player -1 or +1, depenging the values entered on the inspector
  198.             //Move the player to the modified position after recoil
  199.             transform.position = positionAfterRecoil;
  200.  
  201.             Debug.Log(recoilRight);
  202.             CurrentWeapon.WeaponInputStart();
  203.             return;
  204.         }
  205.  
  206.         /// <summary>
  207.         /// Causes the character to stop shooting
  208.         /// </summary>
  209.         public virtual void ShootStop()
  210.         {
  211.             // if the Shoot action is enabled in the permissions, we continue, if not we do nothing
  212.             if (!AbilityPermitted
  213.                 || (CurrentWeapon == null)
  214.                 || (_movement == null))
  215.             {
  216.                 return;    
  217.             }      
  218.  
  219.             if (_movement.CurrentState == CharacterStates.MovementStates.LadderClimbing && CurrentWeapon.WeaponState.CurrentState == Weapon.WeaponStates.WeaponIdle)
  220.             {
  221.                 return;
  222.             }
  223.  
  224.             if ((CurrentWeapon.WeaponState.CurrentState == Weapon.WeaponStates.WeaponReload)
  225.                 || (CurrentWeapon.WeaponState.CurrentState == Weapon.WeaponStates.WeaponReloadStart)
  226.                 || (CurrentWeapon.WeaponState.CurrentState == Weapon.WeaponStates.WeaponReloadStop))
  227.             {
  228.                 return;
  229.             }
  230.  
  231.             if ((CurrentWeapon.WeaponState.CurrentState == Weapon.WeaponStates.WeaponDelayBeforeUse) && (!CurrentWeapon.DelayBeforeUseReleaseInterruption))
  232.             {
  233.                 return;
  234.             }
  235.  
  236.             if ((CurrentWeapon.WeaponState.CurrentState == Weapon.WeaponStates.WeaponDelayBetweenUses) && (!CurrentWeapon.TimeBetweenUsesReleaseInterruption))
  237.             {
  238.                 return;
  239.             }
  240.  
  241.             CurrentWeapon.TurnWeaponOff();
  242.         }
  243.  
  244.         /// <summary>
  245.         /// Reloads the weapon
  246.         /// </summary>
  247.         protected virtual void Reload()
  248.         {
  249.             if (CurrentWeapon != null)
  250.             {
  251.                 CurrentWeapon.InitiateReloadWeapon ();
  252.             }
  253.         }
  254.        
  255.         /// <summary>
  256.         /// Changes the character's current weapon to the one passed as a parameter
  257.         /// </summary>
  258.         /// <param name="newWeapon">The new weapon.</param>
  259.         public virtual void ChangeWeapon(Weapon newWeapon, string weaponID, bool combo = false)
  260.         {
  261.             // if the character already has a weapon, we make it stop shooting
  262.             if (CurrentWeapon!=null)
  263.             {
  264.                 if (!combo)
  265.                 {
  266.                     ShootStop();
  267.                     MMAnimator.UpdateAnimatorBool(_animator, CurrentWeapon.EquippedAnimationParameter, false, _character._animatorParameters);
  268.                     Destroy(CurrentWeapon.gameObject);
  269.                 }              
  270.             }
  271.  
  272.             if (newWeapon != null)
  273.             {          
  274.                 if (!combo)
  275.                 {
  276.                     CurrentWeapon = (Weapon)Instantiate(newWeapon, WeaponAttachment.transform.position + newWeapon.WeaponAttachmentOffset, WeaponAttachment.transform.rotation);
  277.                 }              
  278.                 CurrentWeapon.transform.parent = WeaponAttachment.transform;
  279.                 CurrentWeapon.SetOwner (_character, this);
  280.                 CurrentWeapon.WeaponID = weaponID;
  281.                 _aimableWeapon = CurrentWeapon.GetComponent<WeaponAim> ();
  282.                 // we handle (optional) inverse kinematics (IK)
  283.                 if (_weaponIK != null)
  284.                 {
  285.                     _weaponIK.SetHandles(CurrentWeapon.LeftHandHandle, CurrentWeapon.RightHandHandle);
  286.                 }
  287.                 // we turn off the gun's emitters.
  288.                 CurrentWeapon.Initialization();
  289.                 CurrentWeapon.InitializeComboWeapons();
  290.                 CurrentWeapon.InitializeAnimatorParameters();
  291.                 InitializeAnimatorParameters();
  292.                 if ((_character != null) && !combo)
  293.                 {
  294.                     if (!_character.IsFacingRight)
  295.                     {
  296.                         if (CurrentWeapon != null)
  297.                         {
  298.                             CurrentWeapon.FlipWeapon();
  299.                             CurrentWeapon.FlipWeaponModel();
  300.                         }
  301.                     }
  302.                 }              
  303.             }
  304.             else
  305.             {
  306.                 CurrentWeapon = null;
  307.             }
  308.         }  
  309.  
  310.         /// <summary>
  311.         /// Flips the current weapon if needed
  312.         /// </summary>
  313.         public override void Flip()
  314.         {
  315.             if (CurrentWeapon != null)
  316.             {
  317.                 CurrentWeapon.FlipWeapon();
  318.                 if (CurrentWeapon.FlipWeaponOnCharacterFlip)
  319.                 {
  320.                     CurrentWeapon.FlipWeaponModel();
  321.                 }
  322.             }
  323.         }
  324.  
  325.         /// <summary>
  326.         /// Updates the ammo display bar and text.
  327.         /// </summary>
  328.         public virtual void UpdateAmmoDisplay()
  329.         {
  330.             if ( (GUIManager.Instance != null) && (_character.CharacterType == Character.CharacterTypes.Player) )
  331.             {
  332.                 if (CurrentWeapon == null)
  333.                 {
  334.                     GUIManager.Instance.SetAmmoDisplays (false, _character.PlayerID);
  335.                     return;
  336.                 }
  337.  
  338.                 if (!CurrentWeapon.MagazineBased && (CurrentWeapon.WeaponAmmo == null))
  339.                 {
  340.                     GUIManager.Instance.SetAmmoDisplays (false, _character.PlayerID);
  341.                     return;
  342.                 }
  343.  
  344.                 if (CurrentWeapon.WeaponAmmo == null)
  345.                 {                  
  346.                     GUIManager.Instance.SetAmmoDisplays (true, _character.PlayerID);
  347.                     GUIManager.Instance.UpdateAmmoDisplays(CurrentWeapon.MagazineBased, 0, 0, CurrentWeapon.CurrentAmmoLoaded, CurrentWeapon.MagazineSize, _character.PlayerID, false);
  348.                     return;
  349.                 }
  350.                 else
  351.                 {
  352.                     GUIManager.Instance.SetAmmoDisplays (true, _character.PlayerID);
  353.                     GUIManager.Instance.UpdateAmmoDisplays(CurrentWeapon.MagazineBased, CurrentWeapon.WeaponAmmo.CurrentAmmoAvailable, CurrentWeapon.WeaponAmmo.MaxAmmo, CurrentWeapon.CurrentAmmoLoaded, CurrentWeapon.MagazineSize, _character.PlayerID, true);  
  354.                     return;
  355.                 }
  356.             }
  357.         }
  358.  
  359.         /// <summary>
  360.         /// Adds required animator parameters to the animator parameters list if they exist
  361.         /// </summary>
  362.         protected override void InitializeAnimatorParameters()
  363.         {
  364.             if (CurrentWeapon == null)
  365.             {   return; }
  366.  
  367.             RegisterAnimatorParameter(CurrentWeapon.WeaponAngleAnimationParameter, AnimatorControllerParameterType.Float);
  368.             RegisterAnimatorParameter(CurrentWeapon.WeaponAngleRelativeAnimationParameter, AnimatorControllerParameterType.Float);
  369.             RegisterAnimatorParameter(CurrentWeapon.IdleAnimationParameter, AnimatorControllerParameterType.Bool);
  370.             RegisterAnimatorParameter(CurrentWeapon.StartAnimationParameter, AnimatorControllerParameterType.Bool);
  371.             RegisterAnimatorParameter(CurrentWeapon.DelayBeforeUseAnimationParameter, AnimatorControllerParameterType.Bool);
  372.             RegisterAnimatorParameter(CurrentWeapon.DelayBetweenUsesAnimationParameter, AnimatorControllerParameterType.Bool);
  373.             RegisterAnimatorParameter(CurrentWeapon.StopAnimationParameter, AnimatorControllerParameterType.Bool);
  374.             RegisterAnimatorParameter(CurrentWeapon.ReloadStartAnimationParameter, AnimatorControllerParameterType.Bool);
  375.             RegisterAnimatorParameter(CurrentWeapon.ReloadStopAnimationParameter, AnimatorControllerParameterType.Bool);
  376.             RegisterAnimatorParameter(CurrentWeapon.ReloadAnimationParameter, AnimatorControllerParameterType.Bool);
  377.             RegisterAnimatorParameter(CurrentWeapon.SingleUseAnimationParameter, AnimatorControllerParameterType.Bool);
  378.             RegisterAnimatorParameter(CurrentWeapon.UseAnimationParameter, AnimatorControllerParameterType.Bool);
  379.             RegisterAnimatorParameter(CurrentWeapon.EquippedAnimationParameter, AnimatorControllerParameterType.Bool);
  380.         }
  381.  
  382.         /// <summary>
  383.         /// Override this to send parameters to the character's animator. This is called once per cycle, by the Character
  384.         /// class, after Early, normal and Late process().
  385.         /// </summary>
  386.         public override void UpdateAnimator()
  387.         {
  388.             if (CurrentWeapon == null)
  389.             {   return; }
  390.  
  391.             MMAnimator.UpdateAnimatorBool(_animator,CurrentWeapon.IdleAnimationParameter,(CurrentWeapon.WeaponState.CurrentState == Weapon.WeaponStates.WeaponIdle),_character._animatorParameters);
  392.             MMAnimator.UpdateAnimatorBool(_animator,CurrentWeapon.StartAnimationParameter,(CurrentWeapon.WeaponState.CurrentState == Weapon.WeaponStates.WeaponStart),_character._animatorParameters);
  393.             MMAnimator.UpdateAnimatorBool(_animator,CurrentWeapon.DelayBeforeUseAnimationParameter,(CurrentWeapon.WeaponState.CurrentState == Weapon.WeaponStates.WeaponDelayBeforeUse),_character._animatorParameters);
  394.             MMAnimator.UpdateAnimatorBool(_animator,CurrentWeapon.UseAnimationParameter,(CurrentWeapon.WeaponState.CurrentState == Weapon.WeaponStates.WeaponDelayBeforeUse || CurrentWeapon.WeaponState.CurrentState == Weapon.WeaponStates.WeaponUse || CurrentWeapon.WeaponState.CurrentState == Weapon.WeaponStates.WeaponDelayBetweenUses ),_character._animatorParameters);
  395.             MMAnimator.UpdateAnimatorBool(_animator,CurrentWeapon.SingleUseAnimationParameter,(CurrentWeapon.WeaponState.CurrentState == Weapon.WeaponStates.WeaponUse),_character._animatorParameters);
  396.             MMAnimator.UpdateAnimatorBool(_animator,CurrentWeapon.DelayBetweenUsesAnimationParameter,(CurrentWeapon.WeaponState.CurrentState == Weapon.WeaponStates.WeaponDelayBetweenUses),_character._animatorParameters);
  397.             MMAnimator.UpdateAnimatorBool(_animator,CurrentWeapon.StopAnimationParameter,(CurrentWeapon.WeaponState.CurrentState == Weapon.WeaponStates.WeaponStop),_character._animatorParameters);
  398.             MMAnimator.UpdateAnimatorBool(_animator,CurrentWeapon.ReloadStartAnimationParameter,(CurrentWeapon.WeaponState.CurrentState == Weapon.WeaponStates.WeaponReloadStart),_character._animatorParameters);
  399.             MMAnimator.UpdateAnimatorBool(_animator,CurrentWeapon.ReloadAnimationParameter,(CurrentWeapon.WeaponState.CurrentState == Weapon.WeaponStates.WeaponReload),_character._animatorParameters);
  400.             MMAnimator.UpdateAnimatorBool(_animator,CurrentWeapon.ReloadStopAnimationParameter,(CurrentWeapon.WeaponState.CurrentState == Weapon.WeaponStates.WeaponReloadStop),_character._animatorParameters);
  401.             MMAnimator.UpdateAnimatorBool(_animator, CurrentWeapon.EquippedAnimationParameter, true, _character._animatorParameters);
  402.  
  403.             if (_aimableWeapon != null)
  404.             {
  405.                 MMAnimator.UpdateAnimatorFloat (_animator, CurrentWeapon.WeaponAngleAnimationParameter, _aimableWeapon.CurrentAngle,_character._animatorParameters);
  406.                 MMAnimator.UpdateAnimatorFloat (_animator, CurrentWeapon.WeaponAngleRelativeAnimationParameter, _aimableWeapon.CurrentAngleRelative,_character._animatorParameters);
  407.             }
  408.             else
  409.             {
  410.                 MMAnimator.UpdateAnimatorFloat (_animator, CurrentWeapon.WeaponAngleAnimationParameter, 0f,_character._animatorParameters);
  411.                 MMAnimator.UpdateAnimatorFloat (_animator, CurrentWeapon.WeaponAngleRelativeAnimationParameter, 0f,_character._animatorParameters);
  412.             }
  413.         }
  414.  
  415.         protected override void OnHit()
  416.         {
  417.             base.OnHit();
  418.             if (GettingHitInterruptsAttack && (CurrentWeapon != null))
  419.             {
  420.                 CurrentWeapon.Interrupt();
  421.             }
  422.         }
  423.  
  424.         protected override void OnDeath()
  425.         {
  426.             base.OnDeath();
  427.             ShootStop();
  428.         }
  429.     }
  430. }
Advertisement
Add Comment
Please, Sign In to add comment