Stamina Not Stopping Speed Change

MissBig

Member
Hi, I have a stamina attribute when you run it decreases the stamina which works fine but when it gets to zero it is meant to stop you running automaticaly until stamina increases so you can run again but it doesn't do this. Maybe I am doing something wrong.


Stamina Attribute on Run.PNG
 
From what I see there is no way to say "do something" when an attribute is a specific value.

Here is a possible solution via code. Attach the following to your character.



--------------------


C#:
using UnityEngine;

public class StaminaMonitor : MonoBehaviour
{
    public Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion m_CharacterLocomotion;
    public string m_AttributeText = "Stamina";

    private Opsive.UltimateCharacterController.Traits.AttributeManager m_AttributeManager;
    private Opsive.UltimateCharacterController.Character.Abilities.SpeedChange m_SpeedChange;

    private void Awake()
    {
        if (m_CharacterLocomotion == null)
        {
            Debug.LogError("ERROR: set 'm_CharacterLocomotion' in the inspector.");

            return;
        }

        m_AttributeManager = GetComponent<Opsive.UltimateCharacterController.Traits.AttributeManager>();

        if (m_AttributeManager == null)
        {
            Debug.LogError("ERROR: 'm_AttributeManager' not found.");
        }
    }

    private void Start()
    {
        Opsive.UltimateCharacterController.Events.EventHandler.RegisterEvent<Opsive.UltimateCharacterController.Traits.Attribute>(m_CharacterLocomotion.gameObject, "OnAttributeUpdateValue", OnAttributeUpdateValue);

        m_SpeedChange = m_CharacterLocomotion.GetAbility<Opsive.UltimateCharacterController.Character.Abilities.SpeedChange>();

        if (m_SpeedChange == null)
        {
            Debug.LogError("ERROR: 'm_SpeedChange' not found.");
        }
    }

    private void OnDestroy()
    {
        Opsive.UltimateCharacterController.Events.EventHandler.UnregisterEvent<Opsive.UltimateCharacterController.Traits.Attribute>(m_CharacterLocomotion.gameObject, "OnAttributeUpdateValue", OnAttributeUpdateValue);
    }

    /// <summary>
    /// Monitor the "Staminia" attribute.
    /// </summary>
    private void OnAttributeUpdateValue(Opsive.UltimateCharacterController.Traits.Attribute attribute)
    {
        // ignore other attributes, IE health, shield
        if (attribute.Name != m_AttributeText) return;

        // if Stamina reaches 0 and 'Speed Change' is enabled - disable it
        if (attribute.Value == 0 && m_SpeedChange.Enabled)
        {
            m_SpeedChange.Enabled = false;
        }

        // if 'Speed Change' is disabled and Stamina reaches 100 - enable Speed Change
        if (!m_SpeedChange.Enabled && attribute.Value == 100)
        {
            m_SpeedChange.Enabled = true;
        }
    }
}
 
Last edited:
Oh, thank you so much. It works. In my game, I am going to be using Adventure creator for interactions menus and inventory. I was going to try in adventure creator the stamina considering I can tap into the speed changes in there but thank you so much for taking your time to do this.
 
Thanks for letting me know - this is a problem with the way the attributes are checking the minimum value. Within Attribute.IsValid change the >= to > on line 143 of AttributeManager.cs.

Code:
return m_Value + valueChange > m_MinValue;
From what I see there is no way to say "do something" when an attribute is a specific value.
That's correct - there aren't any callbacks when a certain value is reached but it is a good idea
 
This works but the only thing is when it goes down to 0 the stamina increases straight away. is there a way to make it wait a certain amount of time before it increases.
 
This works but the only thing is when it goes down to 0 the stamina increases straight away. is there a way to make it wait a certain amount of time before it increases.
The attribute manager will update the value after the interval specified. I can see there being a slightly on again off again situation where the attribute hits 0, increases a tiny bit, then gets used again immediately with the character starting to sprint. Even if there was a slight delay when the attribute increased this wouldn't prevent this situation so I'd like to figure out a different way to handle it. Maybe two values, a min value and then one that specifies a min starting value? So the character will stop sprinting when the attribute reaches 0, but then not be able to start again until the attribute reaches x?
 
i'd change the values to increase slower based on the amount of time you want it to reach 100. although, I'm biased since my attribute stuff is all time based.

edit: +1 for the callback idea! it'd be nice to have the ability to not have to code anything. ie bolt/playmaker
 
The attribute manager will update the value after the interval specified. I can see there being a slightly on again off again situation where the attribute hits 0, increases a tiny bit, then gets used again immediately with the character starting to sprint. Even if there was a slight delay when the attribute increased this wouldn't prevent this situation so I'd like to figure out a different way to handle it. Maybe two values, a min value and then one that specifies a min starting value? So the character will stop sprinting when the attribute reaches 0, but then not be able to start again until the attribute reaches x?

Will there be an update which fixes this?

i'd change the values to increase slower based on the amount of time you want it to reach 100. although, I'm biased since my attribute stuff is all time based.

edit: +1 for the callback idea! it'd be nice to have the ability to not have to code anything. ie bolt/playmaker

I'm pretty sure people had done similar with playmaker already so they never had to code. I have however changed values but it never works. I might have to try playmaker for something like this. I'm not sure if bolt is integrated with opsive controllers.
 
Hi

Some time ago i made an ability to sprint that works as I understand you request.


You can set:

- Minimal stamina to start running (Checked on "Can Start Ability").
- Minimal stamina to execute (When value is below, the ability will be stopped)
- Rate of consumption (How often the ability will reduce stamina)
- Amount consumed per rate. (How much stamina need to be reduced each time)

(The stamina to start and execute are different, to add a little cooldown when your character is out of stamina)

Hope it's useful!
 
Thanks for letting me know - this is a problem with the way the attributes are checking the minimum value. Within Attribute.IsValid change the >= to > on line 143 of AttributeManager.cs.

Code:
return m_Value + valueChange > m_MinValue;

That's correct - there aren't any callbacks when a certain value is reached but it is a good idea

Hello! Looks like this has not been changed in code.
Is this still considered a bug?
 
Top