How do i make hitstun for a 2D fighting game (C# Unity2D)

I am assuming you want your player not beeing able to move for a certain amount of time after beeing hit. If this is what you want here it is:

You need a few variables and implement it where you handle your movement this is important! I think the Variables are self-explained.

public bool isStun; public bool gotHit; public float timeStunnedValue; private float timeStunned;

public void Start() { timeStunned= timeStunnedValue ; } public void Update () { if(gotHit) { if(timeStunned <= 0) { isStun = false; gotHit = false; timeStunned = timeStunnedValue ; } else { isStun = true; timeStunned - = Time. deltaTime; } }

} All you have to do now is to make an if statement for your movement that it only runs when isStun equals false and whenever you get hit you simply need to set gotHit to true. After a certain time (timeStunnedValue) you will be able to move again. If you want to play Animations you can simply set a bool in the animation tree and use the isStun variable to assign it with an animator you need to link in the insepcetor. if there's any question just ask. Hope I could helped you!!!

/r/gamedev Thread