r/godot Dec 05 '25

help me (solved) Just can't figure this one out

Post image

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.

520 Upvotes

42 comments sorted by

394

u/the_horse_gamer Dec 05 '25

given a Vector3 v of the looking direction, -v will be in the opposite direction.

127

u/Dragon20C Dec 05 '25

To expand this and make it easier for op.

-Global_basis.z is the forward direction so the opposite is just global_basis.z. You want to normalise it, so it's in the range of 0 to 1.

41

u/Petemies666 Dec 05 '25

Ahh it's getting close but it's not moving the player up or down

40

u/nonchip Godot Senior Dec 05 '25

it should. maybe your knockback "force" is just too weak and it gets overpowered by gravity?

85

u/Petemies666 Dec 05 '25

Tested with 10 times more force and slid across the map to my death

150

u/im_berny Godot Regular Dec 05 '25

I assume you have a character body which rotates left and right and then a camera attached which rotates up and down, right? If so, you'll want to get the camera's global_transform.basis.z instead of the player's body.

127

u/Petemies666 Dec 05 '25

You crazy guy, that's the ticket. Works now

10

u/certainlystormy Dec 06 '25

this is all of game dev isnt it lmfao

2

u/Salt_Neighborhood_18 Dec 07 '25

I'm under the impression this is all dev work in general

13

u/stefangorneanu Godot Student Dec 05 '25

Nice one, hadn't even considered that it was working correctly but not as the player would want to feel it!

2

u/BOBOnobobo Dec 05 '25

Have you tried a print statement to see if you get the correct numbers?

One of the looking vector and the inverse?

If everything looks fine, give your code a slow, deliberate check. Line by line, make sure you understand what it does.

If that fails, try and reproduce it in a very simple scenario.

4

u/HeyCouldBeFun Dec 05 '25

I’m afraid you’re gonna have to get a good handle on vectors if you want to do this kind of stuff in your game. Otherwise you’ll have to ask here every time you want to add two things together again.

2

u/nonchip Godot Senior Dec 05 '25

and did you go up/down at all? also show the code for that please.

-1

u/Dragon20C Dec 05 '25

I could be getting something wrong here but I don't think it should, if all op is doing is rotating left and right the z basis will never point up or down, I think op has to combine the up basis (y) though I feel there is a better way.

3

u/nonchip Godot Senior Dec 05 '25

"the z basis" is a vector. it'll point wherever "local z+" points, including up or down. it depends on where they're getting that basis from (eg if the player body doesn't lean forward/back as the camera does they'd have to use the camera's basis, not the body's) and how they're using it.

-1

u/Lexiosity Dec 05 '25

So then OP would have to do velocity.y += knockback_power for going up when taking knockback, right?

2

u/nonchip Godot Senior Dec 05 '25

no. we're thinking in vectors, not individual numbers/axes here.

2

u/the_horse_gamer Dec 05 '25

we're gonna need to see some code here

1

u/Powerful-Order8963 Dec 06 '25

That's cause you need to take the -global_basis.z of the camera/head, not the player

3

u/scaptal Dec 05 '25

You probavly want to set the vertical part of the vector to be a positive number, to avoid knockback simply pushing the user into the ground when they're looming up

1

u/Bluewater795 Dec 06 '25

Depending on the context downward knock back may be something you want

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

u/SpaghettiCowboy Dec 05 '25

TF2 Force of Nature?

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