48 lines
1.1 KiB
C#
48 lines
1.1 KiB
C#
using Unity.Netcode;
|
|
using UnityEngine;
|
|
|
|
namespace HelloWorld
|
|
{
|
|
public class HelloWorldPlayer : NetworkBehaviour
|
|
{
|
|
public NetworkVariable<Vector3> Position = new NetworkVariable<Vector3>();
|
|
|
|
public override void OnNetworkSpawn()
|
|
{
|
|
if (IsOwner)
|
|
{
|
|
Move();
|
|
}
|
|
}
|
|
|
|
public void Move()
|
|
{
|
|
if (NetworkManager.Singleton.IsServer)
|
|
{
|
|
var randomPosition = GetRandomPositionOnPlane();
|
|
transform.position = randomPosition;
|
|
Position.Value = randomPosition;
|
|
}
|
|
else
|
|
{
|
|
SubmitPositionRequestServerRpc();
|
|
}
|
|
}
|
|
|
|
[ServerRpc]
|
|
void SubmitPositionRequestServerRpc(ServerRpcParams rpcParams = default)
|
|
{
|
|
Position.Value = GetRandomPositionOnPlane();
|
|
}
|
|
|
|
static Vector3 GetRandomPositionOnPlane()
|
|
{
|
|
return new Vector3(Random.Range(-3f, 3f), 1f, Random.Range(-3f, 3f));
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
transform.position = Position.Value;
|
|
}
|
|
}
|
|
} |