Comments (1)
Hi @ManuelRauber,
Thanks for using UnityHFSM!
If I understand your scenario correctly, you're having difficulties modelling the behaviour of the weapon. While in idle, the weapon should circle around the player. When the attack button is pressed (and sufficient time has passed since the last attack, which is determined by the weapon cooldown), it should shoot an enemy. After the attack, while in cooldown, the weapon should circle around the player again.
There are two ways that this could be implemented.
The first approach is to have three states: Idle
, Shoot
, Cooldown
. The advantage of this is that you have a dedicated cooldown state which allows you to e.g. play animations / sounds / etc. The state machine would then look like this:
stateDiagram-v2
[*] --> Idle
Idle --> Shoot: Attack Btn Pressed
Shoot --> Cooldown
Cooldown --> Idle: Cooldown Passed
To make the weapon circle around the player during cooldown, you call the same movement function as you do in Idle
in the OnLogic
of the Cooldown
state.
There are two ways how we can handle the Cooldown Passed
condition from the above state diagram:
- We can use a
TransitionAfter
transition between theCooldown
and theIdle
states. This transition only triggers after the 1s cooldown has elapsed. However, the problem with this approach is that the weapon may need some time to attack (e.g. stop moving, target enemy, play shoot animation, ...), essentially increasing the duration of the cooldown to 1s + time to attack. - To mitigate this, we can keep track of when the player attacked in a variable. In the condition of the
Cooldown
toIdle
transition we simply check whether enough time has passed. The advantage with this approach is that it is not affected by how long the attack state needs.
The second approach is to use only two states: Idle
and Attack
. The trick here is that the cooldown phase need not be a separate state, and can instead be modelled a different way. We can use the same idea from above (see point 2): use a variable to store when the player last attacked to essentially keep track of the cooldown in the background.
This solves your circling-while-in-idle-and-cooldown problem by removing the cooldown state altogether.
stateDiagram-v2
[*] --> Idle
Idle --> Attack: Attack Btn Pressed <br> & Cooldown Passed
Attack --> Idle
Which approach is better for you depends on your exact game and what your weapons should do when they shoot.
As a small tip, remember that you can also use coroutines for the Attack
/ Shoot
states if you have complex game logic / multiple steps that need to be run by using a CoState
.
from unityhfsm.
Related Issues (20)
- Feature request: onEnter and onExit on non-root StateMachines HOT 2
- feature request: OnActiveStateChanged event? HOT 3
- Feature request: add OnTransition callback to Transition class HOT 9
- Name of Test asmdef can conflict with project HOT 1
- StateMachine<T> and StateMachine needs a common StateMachine base type HOT 6
- How to return to the previous sub-state of a Hierarchical FSM? (2.0) HOT 2
- Is there a StateChanged changed event for when any transition occurs, even a sub-machine one? HOT 3
- Trigger/Action should use a separate type
- Documentation request: Explain what HybridStateMachine is HOT 1
- Sample code can not compile in version 2.0 HOT 2
- Mention Support for Generic State Identifiers Higher Up in README HOT 1
- How to visualize multi-layer state machines HOT 1
- How to use AddTransition in two different TStateId based by enum? HOT 1
- [Question] Any node editor for UnityHfsm?Or other Editor Tool ? HOT 1
- [Question] Support for parallel states HOT 2
- Perform logic in FixedUpdate or LateUpdate?
- More Samples/Explaination On Class-based architecture HOT 1
- Are there reasons to not use this library outside of Unity? HOT 1
- [Feature Request] States that use tasks instead of Coroutines HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from unityhfsm.