Error in Bullet spawn multiplayer

Godot Version

4.3
https://youtu.be/b6b4l1693eg?feature=shared&t=15

Question

as you can see in the video the bullet goes anywhere

@rpc("any_peer", "call_local", "reliable")
func _attack() -> void:
	var sender_id = multiplayer.get_remote_sender_id()
	print("attac called by ", sender_id)
	laser_sound.stop()
	laser_sound.play()
	var camera = player.camera
	var space_state = camera.get_world_3d().direct_space_state
	var screen_center = get_viewport().size / 2

	var origin = camera.project_ray_origin(screen_center)
	var end = camera.project_ray_normal(screen_center) * 2300
	# bullet
	var balita = bullet.instantiate()
	
	print(balita.name)
	
	#muzzle.add_child(balita)
	var query = PhysicsRayQueryParameters3D.create(origin, end)
	query.collide_with_bodies = true
	var result = space_state.intersect_ray(query)
	if result:
		get_parent().add_child(balita)
		balita.look_at(result.get("position"), Vector3.UP)
		print(result)
		# pinta rojo el lugar donde llega la pala
		#_test_raycast.rpc(result.get("position"))
		print("collide!")
		var hit_player = result.get("collider")
		# rpc_id to call it only in the player that got hit
		print(hit_player)
		var email = null 
		if MPlay.player_data:
			email = MPlay.player_data['email']
			
		if hit_player is CharacterBody3D:
			
			print("Raycast hit a Character3D!")
			hit_player.receive_damage.rpc_id(hit_player.get_multiplayer_authority(), email)
	await get_tree().create_timer(3).timeout
	balita.queue_free()

When it comes to remote player instances whose camera is this referring to?

I have a machine state kind of system, the player is referenced by the @onready var player = $“…” at the state machine

Where is player.camera assigned? It’s not in the image shared.

My guess is that it somehow gets the active camera on remote instances. Since it looks like it’s going from the player to the world origin, or player to the active camera.

I would avoid using the camera projection and just use the camera global origin and it’s global -z basis vector as the normal.

var origin = camera.global_position
var end = camera.global_transform.basis.z * 2300
2 Likes

I finally figured it out, the camera was correct, but it the rotation and position of it where fixed on the other clients . I had to add those values to the multiplayer synchronizer and it works perfectly. Thanks!

1 Like

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