Hi guys, I made a project with a car controller and I want to implement the multiplayer logic.
First I used the client Transform Networks and everything worked fine, but now I would to use an authorative Server.
In my script I have a function called Move:
private void MovePlayer()
{
if (!IsOwner) return;
GetInput();
HandleMotor();
HandleSteering();
UpdateWheels();
}
private void FixedUpdate()
{
if (IsOwner)
{
MovePlayer();
}
}
I’m not sure about how to procede, initially I tried to use the clientRPC but in any case the client does not move.
Ok little update, I tried removing my script and using one based on the Character Controller and serverRPC and it actually works. This is a part of this script:
void Update()
{
if (IsClient && IsOwner)
{
ClientInput();
}
ClientMoveAndRotate();
}
private void ClientMoveAndRotate()
{
if (networkPositionDirection.Value != Vector3.zero)
{
characterController.SimpleMove(networkPositionDirection.Value);
}
if (networkRotationDirection.Value != Vector3.zero)
{
transform.Rotate(networkRotationDirection.Value, Space.World);
}
}
private void ClientInput()
{
// left & right rotation
Vector3 inputRotation = new Vector3(0, Input.GetAxis("Horizontal"), 0);
// forward & backward direction
Vector3 direction = transform.TransformDirection(Vector3.forward);
float forwardInput = Input.GetAxis("Vertical");
Vector3 inputPosition = direction * forwardInput;
// let server know about position and rotation client changes
if (oldInputPosition != inputPosition ||
oldInputRotation != inputRotation)
{
oldInputPosition = inputPosition;
UpdateClientPositionAndRotationServerRpc(inputPosition * walkSpeed, inputRotation * rotationSpeed);
}
}
[ServerRpc]
public void UpdateClientPositionAndRotationServerRpc(Vector3 newPosition, Vector3 newRotation)
{
networkPositionDirection.Value = newPosition;
networkRotationDirection.Value = newRotation;
}
In this case networkPositionDirection and networkRotationDirection are NetworkVariables.
Now, my question is, how I can move a player not based on the Character Controller, but based on stuff like MotorTorque and WheelColliders? I have to pass them like NetworkVariables?