I made/trying to make a melee two-dimensional game in Unity. The enemy can attack the player no problem, but the other way around is very finicky and unreliable. The enemy has both a ridged body and a box collider, and the same with the player.
This code is all on the enemy code: https://pastebin.pl/view/a6d99e0d
CodePudding user response:
Because I do not know the specific project, I can not give a specific answer.
But I can provide a similar dilemma I have encountered before. My input was not responded in time in the game. When the player pressed after the jump key, the protagonist may not have been be able to jump in time. When I changed the FixedUpdate in the code to Update, the problem was solved.
Of course, my approach is flawed, but I hope my problem can be addressed to the subject. Bring some tips and good luck!
CodePudding user response:
You're validating input, and updating physics both in the void OnTriggerEnter
method. Physics updates much differently than the regular old Update()
method does.
Your physics checks are already completed (I guess in this case it could be either or).
By calling Input.GetButtonDown()
in this method, it has to grab the exact frame that button mashed down to return true.
The problem is, physics don't Update in the same way. Actually, you aren't even running a physics check this frame. That button just got completely ignored. Dang.
You have to come up with a better way to structure your code to process that input. Find a way that allows you check if the player is attacking, without relying on input. Cough state pattern.
See also MonoBehaviour.FixedUpdate().