Weapon kickback in 3D

Godot Version

Godot-4

Question

I’m trying to make a sort of shotgun jump mechanic in my game where whenever you shoot the shotgun you get pushed in the opposite direction of the camera, allowing you to boost yourself up onto higher platforms or make further jumps. how do I do this? I’m completely lost in vector math.

more specifically, how do I find the “backwards” to the camera direction and then apply that to the velocity of a node, multiplied by some force value or something like that?

I believe you can do that with something like

#to get the direction
var kickbackdir = -$camera.transform.basis.z

you can either do

#this will set your velocity
velocity = kickbackdir * forcevalue

or

#this will just add the velocity on
velocity = velocity + kickbackdir * forcevalue

or maybe even

if Input.is_action_just_pressed.shoot
	#checking if velocity and kickback are opposites
	if velocity.x > 0 and kickbackdir.x < 0
		velocity.x = 0
	elif velocity.x < 0 and kickbackdir.x > 0
		velocity.x = 0
	#repeat for y and z, don't forget to do velocity.y and velocity.z for = 0
	
	velocity = velocity + kickbackdir * forcevalue

hopfully this helps you

this doesn’t seem to be working as the inverse of the camera direction?

sorry I was doing this of memory
try .global_transform.basis.z?

THats it! thanks so much, man

1 Like