Stamina Reduction by State Script
Jan 19, 2019 19:47:22 GMT
komposite, ejlnmusic, and 3 more like this
Post by shadex on Jan 19, 2019 19:47:22 GMT
I know some people have wanted to make heavy attacks cost more stamina. I ran into this problem and wrote the script. Simply attach it to a State, configure the numbers and your done.
This script is made to reduce stamina based on state. To attach this script, go into the animator, and find an attack (Will work on any state, not just attacks). Click on the State, and click "Add Behavior", then add the script. Configure and then your done. This has no requirements, and can be used with or without vMeleeAttackControl or any other StateMachineBehavior scripts. Enjoy!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Invector.vCharacterController;
public class StaminaCostBehavior : StateMachineBehaviour {
/*
* Place on any state. When the state is activated, it will reduce the stamina by StaminaReduction amount
* Accumlative is an stamina drain over time.
* Stamina Recovery Delay is how many seconds till stamina recovery kicks in. This only applies
* to the current state. A roll, attack, jump, sprint or any stamina using action will reset to default
* values.
* PermanentChangeStaminaRecovery changes how fast stamina is recovered. This will permanently change
* the value. Currently it doesn't store the default value. Use with care
*/
private vThirdPersonController HeroBase;
[Tooltip("Reduces Stamina by X Amount")]
public float StaminaReduction;
[Tooltip("Stamina Cost Over Time")]
public bool Accumlative;
[Tooltip("Temp Delay till you recover stamina. Only for this State")]
public float Stamina_Recovery_Delay;
[Tooltip("Changes how fast Stamina is recovered. Lasts until overwritten")]
public float PermanentChangeStaminaRecovery;
// public GameObject player;
// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
HeroBase = animator.GetComponentInChildren<vThirdPersonController>();
if (StaminaReduction != 0)
{
HeroBase.ReduceStamina(StaminaReduction, Accumlative);
}
if (Stamina_Recovery_Delay != 0)
{
HeroBase.currentStaminaRecoveryDelay = Stamina_Recovery_Delay;
}
if (PermanentChangeStaminaRecovery != 0)
{
HeroBase.staminaRecovery = PermanentChangeStaminaRecovery;
}
}
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
//override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
//
//}
// OnStateExit is called when a transition ends and the state machine finishes evaluating this state
//override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
//
//}
// OnStateMove is called right after Animator.OnAnimatorMove(). Code that processes and affects root motion should be implemented here
//override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
//
//}
// OnStateIK is called right after Animator.OnAnimatorIK(). Code that sets up animation IK (inverse kinematics) should be implemented here.
//override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
//
//}
}