Godot Version
4.4.stable
Question
The rotation of my gun doesn’t synchronize with the rest of the players
player.gd
extends CharacterBody2D
class_name Player
@onready var dashduration = $dashduration
@onready var dashcooldown = $dashcooldown
@export var normal_speed = 300.0
@export var dash_speed = 600.0
var speed = normal_speed
var is_dashing = false
var max_health = 100
var dash_available = true
@export var health : int
signal player_death
var player_dead = false
@export var scale_player : float
@onready var arm_left = $Player/Skeleton/Skeleton2D/hip/chest/arm_left
@onready var arm_right = $Player/Skeleton/Skeleton2D/hip/chest/arm_right
@onready var marker2d_arm : Marker2D = $Player/Skeleton/Skeleton2D/hip/chest/arm_right/hand_right/Marker2D
@onready var skeleton : Skeleton2D = $Player/Skeleton/Skeleton2D
@onready var gun_handle_marker2d : Marker2D = $Gun/GunHandle
@onready var gun : Node2D = $Gun
var flip_h : bool = false
@onready var polygons : Node2D = $Player/polygons
@onready var gun_damage = $Gun.damage
var mouse_position
func _ready():
polygons.visible = true
$Gun/Sprite2D.visible = true
$CollisionShape2D.disabled = false
if is_multiplayer_authority():
var camera = Camera2D.new()
add_child(camera)
camera.make_current()
set_physics_process(is_multiplayer_authority())
func _physics_process(delta: float) -> void:
$Mouse.global_position = get_global_mouse_position()
if health == 0 and player_dead == false:
player_dead = true
polygons.visible = false
$Gun/Sprite2D.visible = false
$CollisionShape2D.disabled = true
health_label()
score.enemy_score += 1
emit_signal("player_death")
elif health == 0 and player_dead == true:
pass
else:
var direction_x := Input.get_axis("ui_left", "ui_right")
if direction_x:
velocity.x = direction_x * speed
else:
velocity.x = move_toward(velocity.x, 0, speed)
var direction_y := Input.get_axis("ui_up", "ui_down")
if direction_y:
velocity.y = direction_y * speed
else:
velocity.y = move_toward(velocity.y, 0, speed)
move_and_slide()
if Input.is_action_just_pressed("wedashing") and dash_available == true and not is_dashing:
start_wedashing()
health_label()
arm_rotation()
func start_wedashing():
is_dashing = true
dash_available = false
speed = dash_speed
dashcooldown.start()
dashduration.start()
func _on_dashduration_timeout() -> void:
is_dashing = false
speed = normal_speed
func _on_dashcooldown_timeout() -> void:
dash_available = true
func _on_area_2d_area_entered(area: Area2D) -> void:
if area.is_in_group("bullets"):
health -= area.damage
func left_arm_rotation():
arm_left.global_position = arm_right.global_position
arm_left.rotation_degrees = arm_right.rotation_degrees + 5
func update_gun_position():
gun.global_position = marker2d_arm.global_position
gun.look_at(get_global_mouse_position())
func arm_rotation():
arm_right.look_at($Mouse.global_position)
arm_right.rotation_degrees = wrap(arm_right.rotation_degrees, 0, 360)
arm_right.rotation_degrees += 180
left_arm_rotation()
update_gun_position()
func _on_gun_left() -> void:
$Player.scale.x = -1
#rotation_degrees = 180
flip_h = true
func _on_gun_right() -> void:
$Player.scale.x = 1
#rotation_degrees = 0
flip_h = false
func health_label():
$CanvasLayer/Label.text = "<3: " + str(health) + "/" + str(max_health)
func _enter_tree():
set_multiplayer_authority(name.to_int())
gun.gd
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()
else:
return
@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
My multiplayer synchronizer