I wanted to add a knock back effect on my melee weapons in the FPS I'm making.
I've tried to make it work by modifying the target speed, but that doesn't work. They don't arc, land in an unnatrual way, and get a speed boost towards you on the first frame of getting hit. Then I tried affecting their velocity, but outside of being able to zero it, I didn't manage to make a lasting impact that way. I can't even get gravity to work with enemies, though it does work on the player.
It's gotten to the point where I can't even precisely describe what my problem is, as I've become completely exhausted and confused by my code. It's a mess and a half.
The biggest issue is getting enemies to land. They don't. Instead, they walk to the ground. Even if I restrict the Velocity.x and Velocity.z (vars referring to Vector3 of course) to 0 until is_on_floor()
is true, the falling speed is abysmal and unrealistic unless I specifically increase it while in the air. This doesn't work because I then can't reset the speed, since resetting it using checks in delta causes them to reset every frame. However, to do so differently, I'd have to cook up a pot of spaghetti.
I've tried processing gravity the way I do it on the player, but it doesn't affect the falling speed in the least. It's nonsensical. With Vector.y = 20
and Gravity = -30
and a physics process as follows:
function velocity.y += gravity * delta
My player controller allows for amazingly responsive landing, but for the enemy it's like I haven't told them to do anything.
I use basic kinematic body enemies, with no rag dolls. Their movement AI is the classic solution supposedly everybody uses:
move_and_slide(direction.normalized() * speed, Vector3.UP)
I beg you, write me a function that would knock back this target with the strength of a variable and explain how it works. I don't have it in me to ask for simple advice on how to get there myself. It's been three days. Apparently, I don't understand anything about how Vector3 works.
Otherwise I'm gonna have to restrict the kinematic body on the Y axis and live with enemies that can't move up ever.