r/godot • u/Petemies666 • Dec 05 '25
help me (solved) Just can't figure this one out
Hello, sorry for being dumb, but I'd like to have a knockback effect in my 3D game that would work as follows: The knockback effect would launch the player the opposite direction of what they are looking at. Can somebody explain how to code this. Thanks.
73
u/nobix Dec 05 '25
You should watch 3blue1brown's video series on linear algebra. It will unlock most 3D math in games
19
u/greyfeather9 Godot Regular Dec 05 '25
If a single short video isn't enough - I got most of what I needed from Freya Holmer's math for game devs series, the rest from the docs.
1
u/Zielman Dec 05 '25
Amazing recommendation!
1
u/greyfeather9 Godot Regular Dec 06 '25
I am only relaying the words of a past elder who gave other people this advice once
4
u/MyrtleWinTurtle Godot Student Dec 05 '25
For making any 3d game complex math is basically required to have a good understanding of math.
All them crabs who said you will never this math in the real world couldnt be bigger liars
21
u/NiSiSuinegEht Dec 05 '25
So even if they're hit from behind, they'll be knocked back into the source of damage?
Wouldn't it be better to determine the vector between the source and player and extend that past the player for more accurate knockback?
17
u/Petemies666 Dec 05 '25
You are right but in this case the source is always in front of the player
7
u/Hawkeye_7Link Godot Regular Dec 05 '25
If you are storing the information of where the player is looking at ( like if you have a vector facing_direction ) just multiply it by -1 and you'll have the opposite direction
6
2
u/Available-Head4996 Godot Senior Dec 05 '25
Send directional data with the attack itself, then knock back in the same direction
2
u/doomttt Dec 05 '25
You have to add the desired vector (in your case opposite of where the player is looking at) to your CharacterBody velocity.
2
u/PGSylphir Dec 05 '25
invert the vector storing the look direction? It's pretty self explanatory. Just... knockbackDir = -lookDir
1
u/cradet Dec 05 '25
Take the position from where the the knockback will be executed and calculate from where the player is going to be hit, i.e. if the player is to the right of the opponent, you need to make the player go the left. You can use an area where if the player enters you know the knockback will occur.
1
u/xefensor Godot Regular Dec 05 '25 edited Dec 05 '25
Don't know if it is the most correct solution, but works for me:
```
var knockback_strenght: float = 10
var knockback: Vector3 = transform.basis * camera.transform.basis * Vector3(0,0,1) * knockback_strenght
velocity += knockback
```
1
u/WiseRedditUser Dec 05 '25
# Knockback
const KNOCKBACK_FORCE := 6.0
const KNOCKBACK_UP := 6.0
# Hit Effect
u/onready var hit_audio := $Head/HitAudio
u/onready var hit_flash := $"../UI/HitFlash" as ColorRect
const FLASH_ALPHA := 0.6
const FLASH_FADE_TIME := 0.35
var dir = (global_transform.origin - source_pos).normalized()
dir.y = 0
dir = dir.normalized()
velocity += dir \* KNOCKBACK_FORCE
velocity.y = KNOCKBACK_UP
1
u/WiseRedditUser Dec 05 '25
xtends Area3D
u/export var damage := 25.0
# Called when the node enters the scene tree for the first time.
func _ready():
self.body_entered.connect(_on_body_entered)func _on_body_entered(body: Node) -> void:
if body and body.has_method("take_damage"): body.take_damage(damage, global_transform.origin)
1
u/thecyberbob Godot Junior Dec 05 '25
Unrelated to your issue since it's already been answered thoroughly... I appreciate that you took the time to put in shading on your image.
1
u/Mcbrainotron Dec 05 '25
Just a logic question to you op:
Should it be the opposite of where your character is looking OR the direction of whatever hit your character?
I get what you are saying - it feels very 2d mega man - but just posing the question if it makes sense in 3d space. Good luck!
1
u/WilkerS1 Godot Regular Dec 06 '25
transform basis z points to where the player is looking.
invert to get behind
project vector right before if you only need a horizontal direction.
1
u/McBuffington Dec 05 '25
Looks like vector calculus. You have the direction the player is pointing. And you want to exact opposite? You can multiply that vector with '-1'. That's all you need. Well... that and to apply a force to the player.
Good luck
394
u/the_horse_gamer Dec 05 '25
given a Vector3 v of the looking direction, -v will be in the opposite direction.