I'm new and I want to make a shotgun knockback the player

Godot Version

4.2.1

Question

hii, I want to do somethings similar to the video. I can already move my player with basic movement but I have no idea how to do knockback for the player in a 2d game. And I can’t find tutorials on youtube. It’s mybe to advance for a beginner idk. I’m new in coding

So I would like a code for the gun and the knockback pleaaase, if needed I can send what i have already code !

It would be easy for a ridigbody2d just call apply_force or apply_impulse in the opposite direction of the weapon.

If you have a character body. You would need to mimick a jump like behavior, based on the direction of the weapon by modifying the velocity manually.

I use a characterbody2D and I have no idea how to do that

What code do you got so far?

And how is your weapon and character laid out in the scene tree?

For the moment I only have basic movement + dash + double jump

I have nothing done for the weapon because I don’t know how to do :sweat_smile:

extends CharacterBody2D

@export var speed = 300 
@export var gravity = 30
@export var jump_force = 800
@export var down_speed = 1500
@export var dash_speed = 900
#@export var jump_buffer_timer = 0.1

#var jump_buffer:bool = false
#var jump_available:bool = true
var dashing = false 
var can_dash = true


var jump_count = 0
var max_jumps = 2
var dash_count = 0
var max_dash = 1

func _physics_process(delta): 
	if !is_on_floor(): 
		velocity.y += gravity 
		if velocity.y > 1000: 
			velocity.y = 1000  


	if Input.is_action_just_released("jump") and velocity.y < 0:
		velocity.y = jump_force / 5


	if is_on_floor():
		jump_count = 0
		dash_count = 0
	
	if Input.is_action_just_pressed("jump"):
		#if jump_available:
		if jump_count < max_jumps:
				jump_count += 1
				velocity.y = -jump_force 
			#else:
				#jump_buffer = true
				



	if Input.is_action_pressed("move_down"): 
		velocity.y = down_speed

	var horizontal_direction = Input.get_axis("move_left","move_right") 
	velocity.x = speed * horizontal_direction 

	if horizontal_direction:
		if dashing:
			velocity.x = dash_speed * horizontal_direction


	if Input.is_action_just_pressed("dash") and can_dash: 
		if dash_count < max_dash:
			dash_count += 1
			dashing = true
			can_dash = false
			$dash_timer.start()
			$dash_again_timer.start()

	move_and_slide() 

#func jump()->void:
	#velocity.y = jump_force
	#jump_available = false

#stop dashing
func _on_dash_timer_timeout():
	dashing = false
func _on_dash_again_timer_timeout():
	can_dash = true
	
	print(velocity)

This is all the code i have on the game, but I will try to add the weapons, I have found a tuto. But I still don’t know how to make the knockback

Quick, semi-related tip: It will be much easier to read your code if you highlight all of it in the post editor, and then press the </> button. That’ll syntax highlight it and make sure the indentation is preserved.

hoo thank you! I edited the message

Awesome!

So, in pseudo code, you’ll need something along the lines of:

if player pressed shoot button:
    shoot_dir = direction of shot
    knockback_dir = -shoot_dir
    velocity = knockback_dir * knockback_speed
    spawn a bullet and set its velocity to shoot_dir * bullet_speed

Where knockback_speed is some large-ish number.

However, with the way your code looks now, the velocity on the X axis will instantly be reset based on horizontal_direction, so you’ll need to check whether or not the player is on the ground before allowing them to move horizontally. You already know the is_on_floor()-function, so I’m sure you can figure that part out.

1 Like

Ok thx ! I will try and if I can’t figure it I will ask for more help !

1 Like

I should do it in the player scripts or Weapon scripts ?

I was thinking player, since it’s the player that should be moved, but maybe the code that spawns bullets and such is better placed in the weapon script.

1 Like

I have some difficulty, I have now the weapon and it move where my mouse is

extends Sprite2D


func _ready():
	set_as_top_level(true)


func _physics_process(delta):
	global_position.x = lerp(global_position.x, get_parent().global_position.x, 0.8) 
	global_position.y = lerp(global_position.y, get_parent().global_position.y + -50, 0.8) 
	var mouse_pos = get_global_mouse_position()
	look_at(mouse_pos)

and I have this in the player scripts

@export var knockback_speed = 1000

var knockback_direction : Vector2
var shoot_direction : Vector2

if Input.is_action_just_pressed("shoot"):
		knockback_direction = -shoot_direction
		velocity = knockback_direction * knockback_speed
		#spawn a bullet and set its velocity to shoot_dir * bullet_speed

For the moment I have this and when I shoot when I fall I lose velocity, and I dont know what to do next.
Finally I would like to still be able to move when I’m not on the floor, not like the video

yey, the youtuber respond and with the help of the discord now it work with for the y axis :

if Input.is_action_just_pressed("shoot"):
		velocity = $ShotGun.transform.x * -knockback_speed * 

but like you said it dont work on the x axis. and in the x axis but is better than before

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