So quick overview, I have a player object and when I press a button I'm shooting a raycast that if it hits another network object it should pull that object to the player that pressed the button. My issue is I'm struggling to totally understand serverRPC, clientRPC, and how to move other objects from a client. Currently what I'm testing with to simplify things is a 3D cube with network obj component and client network transform, it moves to where I want after pressing the button, but then snaps back to where it was. The code seems to work however, if a non-host client presses the button to pull another player, but I don't think it should be working. Also keep in mind, the player objs will have the character controller on them as well (which is why I'm disabling that temporarily if it exists)
Code thats doing the lerp (script is attached to players that are pulling objs):
IEnumerator LerpPosition(NetworkObject networkObject, Vector3 targetPosition, float pullSpeed)
{
float journeyLength = Vector3.Distance(networkObject.transform.position, targetPosition);
Debug.Log("len: " + journeyLength);
float startTime = Time.time;
float journeyTime = journeyLength / pullSpeed;
Debug.Log("NO POS: " + networkObject.transform.position);
while (Time.time < startTime + journeyTime)
{
float fracComplete = (Time.time - startTime) / journeyTime;
Vector3 newPosition = Vector3.Lerp(networkObject.transform.position, targetPosition, fracComplete);
//print("new pos: " + newPosition);
networkObject.gameObject.transform.GetChild(0).gameObject.GetComponent<ServerMove>().RequestMoveClientRpc(newPosition);
yield return null;
}
networkObject.gameObject.transform.GetChild(0).gameObject.GetComponent<ServerMove>().RequestMoveClientRpc(targetPosition);
if (networkObject.transform.GetChild(0).gameObject.TryGetComponent<CharacterController>(out CharacterController cc))
{
cc.enabled = true;
}
networkObject.GetComponent<NetworkObject>().ChangeOwnership(networkObject.NetworkObjectId);
}
Functions that are being called (script is attached to object being pulled):
[ServerRpc(RequireOwnership = false)]
public void RequestMoveServerRpc(Vector3 newPosition)
{
// Server processes the move request
MoveObject(newPosition);
}
[ServerRpc(RequireOwnership = false)]
public void RequestRotateServerRpc(Quaternion newRotation)
{
// Server processes the rotate request
RotateObject(newRotation);
}
[ClientRpc]
public void RequestMoveClientRpc(Vector3 newPosition)
{
MoveObject(newPosition);
}
private void MoveObject(Vector3 newPosition)
{
// Move the object on the server
networkTransform.transform.position = newPosition;
transform.position = newPosition;
//networkTransform.Teleport(newPosition, networkTransform.transform.rotation, networkTransform.transform.localScale);
//networkTransform.SetState(newPosition, networkTransform.transform.rotation, networkTransform.transform.localScale);
}
I've also tried just setting the transform, or setting the client transform instead of using rpcs but that doesn't seem to work either. Help fixing the code would be great, but help understanding why it doesn't work and why it has to be another way would be even better. Thank you so much in advance!