#drag and drop mechanics
1 messages · Page 1 of 1 (latest)
This is not going to work. OnDragBegin will need to check m_isMovingServer. If this has a client network object then it will need to change ownership on the object. Otherwise, you can use an [RPC(SendTo.Everyone)] in order to move the thing
using Unity.Netcode;
using UnityEngine;
using UnityEngine.EventSystems;
namespace NetworkingTest
{
public class MoveObject : NetworkBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
{
[SerializeField] private LayerMask hitLayer;
private Camera m_camera;
private bool m_isMovingClient;
private readonly NetworkVariable<bool> m_isMoving = new();
private void Awake() => m_camera = Camera.main;
public void OnBeginDrag(PointerEventData eventData)
{
if (!m_isMoving.Value)
{
m_isMovingClient = true;
BeginDragRpc();
}
}
public void OnDrag(PointerEventData eventData)
{
if (!m_isMovingClient)
{
return;
}
Vector3 position;
Ray ray = m_camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit, 200f, hitLayer.value))
{
position = hit.point;
}
else
{
Vector3 randomPos = Random.insideUnitSphere;
randomPos.y = 0;
position = randomPos;
}
SetPositionRpc(position);
}
public void OnEndDrag(PointerEventData eventData)
{
if (m_isMovingClient)
{
m_isMovingClient = false;
EndDragRpc();
}
}
[Rpc(SendTo.Server)]
private void BeginDragRpc() => m_isMoving.Value = true;
[Rpc(SendTo.Server)]
private void EndDragRpc() => m_isMoving.Value = false;
[Rpc(SendTo.Server)]
private void SetPositionRpc(Vector3 position) => transform.position = position;
}
}
@strange arrow this works but there is a delay on the client side
how can i fix it
You would need to either use the Anticipation system in 1.9.1+ or you can create your own client prediction system
https://docs-multiplayer.unity3d.com/netcode/current/advanced-topics/client-anticipation/
Client anticipation is only relevant for games using a client-server topology.