Knockback relative to the opponent's player in 3D

Godot Version



And greetings again. So I decided once again to rewrite it so that it would perhaps become clear to me the truth is difficult for an emergency translator to try to explain what I myself do not understand well, but to the point
I have a code for pushing the player away, it seems like everything is fine for me, that’s what to treat it

@export var enemy_path := "../enemis/enemy"

var enemy = null

var knockback_power = 500
var knockback_vector := Vector2.ZERO

func _ready() -> void:
enemy = get_node(enemy_path)

func knockbacking():
	var knockback_direction = igrok.global_position - enemy.global_position
	knockback_direction *= knockback_power
	knockback_direction *= Vector3(1, 0, 1)
	velocity = knockback_direction
func hurt(hit_point):
	health -= hit_point
	print("Health is: ", health)
	$CanvasLayer/ProgressBar.value = health

But as I understand it, there is a problem that now the location of the enemy is determined by the location of the node . And what if I have several different enemies, it turns out that this is not a working option. I need to track the enemy in a different way, or I can just update the node somehow. I honestly don’t know, I’ve tried a bunch of different options, but I haven’t come up with anything.
I just don’t even know which end of the problem is that initially I didn’t do it right and I need to calculate the impact vector in some other way, or can I somehow change the enemy_path in advance, I tried to reformulate the question more clearly, but I don’t know if it became cleare thank you so much for your concern

You’ll want to get a reference to the specific enemy that hit you, for example if the enemy script is the one calling player_node.hurt(some_damage) it could instead do something like player_node.hurt(self, some_damage)
And in the player script

func hurt(who_hit_me, hit_point):

func knockbacking(who_hit_me):
    var knockback_direction = igrok.global_position - who_hit_me.global_position
1 Like

it really worked, thanks a lot, huge, I’ve been struggling with this for a week trying different solutions, and as always 2 lines. I have one more question, I don’t know if you know, but now the player’s repulsion looks jerky like a teleport, rather how to do it smoothly, I tried to add lerp but something didn’t work out either

Is the player being moved every frame with move_and_slide? If not you’ll probably want to do that in _physics_process, don’t call move_and_slide in the knockback function. (there’s an example that does it as a template if you add a script to a CharacterBody3D it should be the default template)

If you set your velocity from input like in that template though then it will go back to 0 or whatever you are inputting immediately. You’ll need to be able to leave the velocity at whatever you set it to until the knockback is over, maybe use a Timer or something.

Or you could try implementing momentum and having your controls add velocity instead of replacing the old velocity, using momentum can be more difficult to get a good feeling player but it’s a lot more flexible in the long run. Good luck!

1 Like

Uh, thanks again for something I understood, then I’ll think about how to do it. If you are not tired of me yet, you can ask one more question that is similar to the first one. If not, then thank you very much

You don’t need to ask if you can ask a question, that’s just asking a question with extra steps :grin:

Well, I don’t want to be intrusive and you can’t imagine how. but I understand the answer is yes. in general, I have a similar system with weapons that are held in my hand so that when moving it turns a little, it trembles and so on. There are lines like this on the game that are for this

@export var weapon_holder : Node3D

and there are already processes that happen to him on the screen when he moves. But the logic of the weapon itself, its animation, etc., is on another script, and there is also a change of weapons through resouces. And when changing weapons to another instance, the movement no longer works for obvious reasons. Is it possible to update weapons on the player in the same way ? I do not know if I explained it clearly, but I tried my best
I tried to add the model to the Resource dictionary, but it does not support the node3D format, so yes, I have a question again whether it is worth changing the approach in principle and abandoning the Resource database, or like last time, you can just add data transfer somehow

Resource is good for storing some generic data, if you’re storing objects like Node3Ds and Meshes and stuff it’s probably better to use a scene instead of a resource.

So you could keep several weapons as packed scenes and instantiate the one you wanted to use, along with it’s models and scripts and all that.
the docs explain some of it here: Nodes and scene instances — Godot Engine (stable) documentation in English
and a more complex example in this page The main game scene — Godot Engine (stable) documentation in English

Uh, I got something. It’s difficult, of course, and I understand that everything needs to be changed in the end , but we’ll think well . And once again, thank you very much for everything and for your concern.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.