Godot Version
4.6
Question
Hello! I’m currently making a game about wizards and guns and I have been stuck on a problem for a day or two now. I’m trying to make it so when the player shoots their gun they get launched in the opposite direction but nothing works. I have watched multiple tutorials and have looked through other godot forums but I can’t get it to work. btw the player is a characterbody2d
extends CharacterBody2D
@onready var player = $".."
@onready var main = $"../.."
@onready var bullet = load("res://scenes/magic_bullet.tscn")
var shoot_cooldown_active = false
var ammo = 10
var reloading_active = false
var spells = ["recoil", "poison","piercing", "damage", "baguette"]
var current_spell = null
var look_at_pos = get_global_mouse_position()
func _process(_delta: float) -> void:
look_at_pos = get_global_mouse_position()
look_at(look_at_pos)
$"../CanvasLayer/ammo".text = str("ammo: ", ammo)
if Input.is_action_just_pressed("spell 1"):
current_spell = spells[0]
print(current_spell)
if Input.is_action_just_pressed("spell 2"):
current_spell = spells[1]
print(current_spell)
if Input.is_action_just_pressed("spell 3"):
current_spell = spells[2]
print(current_spell)
if Input.is_action_just_pressed("spell 4"):
current_spell = spells[3]
print(current_spell)
if Input.is_action_just_pressed("spell 5"):
current_spell = spells[4]
print(current_spell)
if Input.is_action_pressed("shoot") and shoot_cooldown_active == false and ammo > 0 and reloading_active == false:
shoot()
if Input.is_action_just_pressed("reload"):
reload()
func shoot():
var bullet_instance = bullet.instantiate()
bullet_instance.global_position = $Marker2D.global_position
bullet_instance.rotation = global_rotation
main.add_child.call_deferred(bullet_instance)
$Shoot_cooldown.start()
shoot_cooldown_active = true
ammo -= 1
player.recoil(look_at_pos)
func reload():
reloading_active = true
$Reload_timer.start()
func _on_shoot_cooldown_timeout() -> void:
shoot_cooldown_active = false
func _on_reload_timer_timeout() -> void:
reloading_active = false
ammo = 10
This is the gun script there isn’t much in it for the recoil
extends CharacterBody2D
const recoil_strength = 10const SPEED = 500.0const JUMP_VELOCITY = -600.0
func _physics_process(delta: float) → void:
if not is_on_floor():
velocity += get_gravity() * delta
if Input.is_action_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
var direction := Input.get_axis("left", "right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
func recoil(dir):
var force = dir * recoil_strength
velocity = force
this is the code for the player
If I’m shooting downwards it sends me flying upwards really far kinda of in a wierd way and if i shoot up I get sent downards really far but if I shoot towards the sides i barely move.
