Godot Version
4.4.stable
Question
I want to synchronize a bullet from a gun. I tried many tutorials but none of them works.
This is the script of my gun
extends Node2D
@onready var pewpew = $pewpew
@onready var bullet_scene = preload("res://Resources/bullet.tscn")
@onready var marker_2d: Marker2D = $Marker2D
@export var scale_gun: float
@onready var selected_material = preload("res://shaders/selected_gun_material.tres")
@onready var not_selected_material = preload("res://shaders/not_selected_gun_material.tres")
var pewpewcooldown = true
var moving = false
var drift
var cadence : float
@export var primary_selected : bool = false
@export var secondary_selected : bool = false
@export var melee_selected : bool = false
signal left
signal right
var current_weapon : String
var automatic : bool = false
var damage = 10
@export var primary_weapon : Weapons:
set(value):
primary_weapon = value
load_weapon()
@export var secondary_weapon : Weapons:
set(value):
secondary_weapon = value
load_weapon()
func _ready() -> void:
if primary_weapon == null and secondary_weapon != null:
current_weapon = "secondary"
elif primary_weapon != null:
current_weapon = "primary"
else:
current_weapon = "melee"
load_weapon()
update_weapon_icons()
func _process(delta: float) -> void:
if is_multiplayer_authority():
rotation_degrees = wrap(rotation_degrees, 0, 360)
rotation()
var jambo = get_parent()
if jambo.velocity.length() > 0:
moving = true
else:
moving = false
if automatic:
if Input.is_action_pressed("shoot") and pewpewcooldown == true:
shoot.rpc()
else:
if Input.is_action_just_pressed("shoot") and pewpewcooldown == true:
shoot.rpc()
selected()
change_weapon()
load_weapon()
if current_weapon == "primary" and primary_weapon == null:
if secondary_weapon != null:
current_weapon = "secondary"
else:
current_weapon = "melee"
load_weapon()
elif current_weapon == "secondary" and secondary_weapon == null:
if primary_weapon != null:
current_weapon = "primary"
else:
current_weapon = "melee"
load_weapon()
@rpc("call_local")
func shoot():
if moving == true:
drift = deg_to_rad(randf_range(-15, 15))
else:
drift = deg_to_rad(randf_range(-5, 5))
pewpewcooldown = false
pewpew.start()
var bullet = bullet_scene.instantiate()
get_parent().add_child(bullet)
bullet.damage = damage
bullet.global_position = marker_2d.global_position
bullet.rotation = rotation + drift
func rotation():
if rotation_degrees > 90 and rotation_degrees < 270:
scale.y = -scale_gun
emit_signal("left")
else:
scale.y = scale_gun
emit_signal("right")
func _on_pewpew_timeout() -> void:
pewpewcooldown = true
func load_weapon():
if current_weapon == "primary" and primary_weapon != null:
if $Sprite2D != null:
$Sprite2D.texture = primary_weapon.texture
damage = primary_weapon.damage
automatic = primary_weapon.automatic
cadence = primary_weapon.cadence
primary_selected = true
secondary_selected = false
melee_selected = false
elif current_weapon == "secondary" and secondary_weapon != null:
if $Sprite2D != null:
$Sprite2D.texture = secondary_weapon.texture
damage = secondary_weapon.damage
automatic = secondary_weapon.automatic
cadence = secondary_weapon.cadence
primary_selected = false
secondary_selected = true
melee_selected = false
else:
if $Sprite2D != null:
$Sprite2D.texture = null
primary_selected = false
secondary_selected = false
melee_selected = true
update_weapon_icons()
func update_weapon_icons():
if $CanvasLayer/Gun_UI/VBoxContainer/PrimaryWeapon != null:
if primary_weapon != null:
$CanvasLayer/Gun_UI/VBoxContainer/PrimaryWeapon.texture = primary_weapon.icon
else:
$CanvasLayer/Gun_UI/VBoxContainer/PrimaryWeapon.texture = null
if $CanvasLayer/Gun_UI/VBoxContainer/SecondaryWeapn != null:
if secondary_weapon != null:
$CanvasLayer/Gun_UI/VBoxContainer/SecondaryWeapn.texture = secondary_weapon.icon
else:
$CanvasLayer/Gun_UI/VBoxContainer/SecondaryWeapn.texture = null
func change_weapon():
if Input.is_action_just_pressed("primary_weapon") and primary_weapon != null:
current_weapon = "primary"
load_weapon()
elif Input.is_action_just_pressed("secondary_weapon") and secondary_weapon != null:
current_weapon = "secondary"
load_weapon()
elif Input.is_action_just_pressed("melee"):
current_weapon = "melee"
load_weapon()
func selected():
if primary_selected == true:
$CanvasLayer/Gun_UI/VBoxContainer/PrimaryWeapon.material = selected_material
elif primary_selected == false:
$CanvasLayer/Gun_UI/VBoxContainer/PrimaryWeapon.material = not_selected_material
if secondary_selected == true:
$CanvasLayer/Gun_UI/VBoxContainer/SecondaryWeapn.material = selected_material
elif secondary_selected == false:
$CanvasLayer/Gun_UI/VBoxContainer/SecondaryWeapn.material = not_selected_material
if melee_selected == true:
$CanvasLayer/Gun_UI/VBoxContainer/Melee.material = selected_material
elif melee_selected == false:
$CanvasLayer/Gun_UI/VBoxContainer/Melee.material = not_selected_material
This is the script of my projectile
extends Area2D
var speed = 100
var damage : int
func _ready():
add_to_group("bullets")
func _process(delta: float) -> void:
position += transform.x * speed * delta
func _on_visible_on_screen_notifier_2d_screen_exited() -> void:
queue_free()
func _on_area_entered(area: Area2D) -> void:
queue_free()
func _on_body_entered(body: Node2D) -> void:
if !is_multiplayer_authority():
return
if body.is_in_group("enemies"):
body.apply_damage(damage)
queue_free()
This is a video showing the trees and what happens