Well, I looked at the code posted, and I do not see anything that persay seems to be giving you the results you are getting, though I’ll admit there are parts of the code where I am not 100% sure what it is doing.
To be honest, I think the code below is a little overly complicated for what you are wanting to achieve. Granted, I have no idea how the project is setup, what it is supposed to do, nor do I know how the project is running. It might be that the code is how should be, but there are a few things in the code I am confused at (which is fine! I just am struggling to understand how the code works).
For example, why are you using OS.get_window_size
to set the motion? To the best of my knowledge, OS.get_window_size
returns the size of the game window, which depending on your project’s window scaling settings, can be entirely different then the size of the game in the window (because of scaling, adapting to different aspect ratios, etc). I would highly suggest using get_viewport_rect().size
( Documentation page ) instead, as it will give you the size of the game viewport, which will give you the size of the game in the window, not the size of the window itself.
I’m just confused and am having some difficulty understanding what the code is doing, which is more my fault than anything else. :smile:
Anyway, back to the issue at hand!
@mdswamp said:
i want enemy to pull back after it gets hurt (on both x and y axis). two characters are kinematic body 2d. i put this code in process function. but it seems after i press punch button and when enemy feels the punch, i should hold the hit key in order to enemy goes back, if i just press and release the hit button, enemy stop after releasing the key, which i think process function is the reason, but if i want to move the code to input function for ex, it does not work right, beside there is no any delta for changing speed of pulling back.
The basic pysudo code for moving a enemy backwards using code similar to what you posted above should be something like the following:
if (animations.get_current_animation() == “Hurt”):
“If the enemy is on one side...“
if (self.get_global_pos().x > enemy.get_global_pos().x):
“We move the player away from the enemy on the X axis and“
“we move the enemy not quite as far (half as far) on the Y axis“
“depending on which fist hit.“
if (enemy_animations.get_current_animation() == “L_Fist”):
“Remember, the move function takes a relative velocity,
“meaning we only need to tell it which direction to go,
“and at what speed”
move(Vector2(1, 0.5) * speed * delta)
elif (enemy_animations.get_current_animation() == “R_Fist”):
move(Vector2(1, -0.5) * speed * delta)
“If the enemy is on the other side...“
else:
“We move the player away from the enemy on the X axis and“
“we move the enemy not quite as far (half as far) on the Y axis“
“depending on which fist hit.“
If (enemy_animations.get_current_animation() == “L_First”):
move(Vector2(-1, 0.5) * speed * delta)
elif (enemy_animations.get_current_animation() == “R_Fist”):
move(Vector2(-1, -0.5) * speed * delta
else:
“Other, non knockback/backwards-movement code here!”
I am not able to test if the code posted will work, and I removed some things (like calculating the normals) which you may want/need for your project.
I just tried to write what I think will give you the result you are looking for in the first way that came to mind.
(I am by no means an expert in GDScript or game development, so the code may not work at all and my advice may be completely wrong. Hopefully it works, but I would not be surprised if I made some mistakes :sweat_smile: )
You should leave the code for pulling the enemy back in _process
(or better yet, put the code in _fixed_process
as then it will be in sync with the physics engine).
The _input
function is ONLY called when there is a input event recieved. This means if you move the movement code to _input
, it will only move the enemy when the player is pressing input, and as soon as the player stops, the enemy will stop moving backwards.
Hopefully the code I posted works and/or gives you an idea on what you need to do to make it work.
Keep in mind that other factors seemingly untreated could be causing issues as well (for example, watching for input and changing the enemy’s movement while the enemy is being knocked back) that could be contributing to the issue.
If possible, I would suggest disabling enemy input and only checking just the knockback/backwards-movement code. You may be able to comment out other lines of code that could be causing issues so you can better debug and work just on the knockback/backwards-movement code.
Hopefully this helps! :smile: