0 votes

I build a multiplayer game. I have 3 scene:Lobby,Game,Player.When a client connect to server, the game start and I use function addchild() to add player node to game scene.
At normal, when I run the project, the meshinstace map in game scene will stay still and don't move when the player move. But when I add network to the game, when i run the project, the meshinstace map move with the player.Can anybody help me to fix it?
That is my game.tscn script:
extends Node
onready var map=$MeshInstance
func _ready():
net.set
ids()
create_players()

func createplayers():
for id in net.peer
id:
createplayer(id)
create
player(net.id)
func createplayer(id):
var p =preload("res://Player.tscn").instance()
add
child(p)
p.initialize(id)

This is the lobby script:
extends Control

func ready():
get
tree().connect("connectedtoserver",self,"connected")
func connected():
if not net.ishost:
rpc("begin
game")
begingame()
remote func begin
game():
gettree().changescene("res://Game.tscn")

This is network script
extends Node

const port=31400
const maxplayer=100
const ip="127.0.0.1"
const offline=false
var id =null
var is
host=false
var peerid=[]
func start
server():
ishost=true
var peer = NetworkedMultiplayerENet.new()
peer.create
server(port,maxplayer)
get
tree().networkpeer=peer
func join
server(serverip):
if offline:
server
ip=ip
var peer=NetworkedMultiplayerENet.new()
peer.createclient(ip,port)
get
tree().networkpeer=peer
func set
ids():
id=gettree().getnetworkuniqueid()
peerid=gettree().getnetworkconnected_peers()
This is player script:
extends KinematicBody

const speed=10
var ismaster=false
var velocity=Vector3()
var accel=5
var mouse
sensitivity=0.5
func ready():
Input.set
mousemode(Input.MOUSEMODE_CAPTURED)

func process(delta):
if Input.is
actionpressed("uicancel"):
Input.setmousemode(Input.MOUSEMODEVISIBLE)
func initialize(id):
name=str(id)
if id==net.id:
ismaster=true
else:
$MeshInstance.get
surfacematerial(0).albedocolor=Color8(255,0,0,255)
func input(event):
if event is InputEventMouseMotion:
rotate
y(deg2rad(-event.relative.xmousesensitivity))
$MeshInstance.rotate
x(deg2rad(-event.relative.y
mousesensitivity))
$MeshInstance.rotation.x = clamp($MeshInstance.rotation.x, -0.8, 0.8)
func _physics
process(delta):
if ismaster:
var dir=Vector3()
var head
basis=$MeshInstance.getglobaltransform().basis
if Input.isactionpressed("front"):
dir+=headbasis.z
if Input.is
actionpressed("back"):
dir-=head
basis.z
if Input.isactionpressed("left"):
dir-=headbasis.x
if Input.is
actionpressed("right"):
dir+=head
basis.x
dir=dir.normalized()
velocity=velocity.linearinterpolate(dirspeed,acceldelta)
move
andslide(velocity)
rpc
unreliable("updatetranslation",translation)
remote func update
translation(trans):
translation=trans

in Engine by (20 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.