I did the points in the Server Authhoritive Setup section but I did not understand some points.
I wrote the parts mentioned in the documentation but I didn’t see the part about what to run and how to do it.
For example, if I have Input Authority in CoherenceInput in the script in the player, I send inputs.
Then the server, that is, the simulatorm, will take over the part of playing the character?
I created a PlayerMovementSimulator script for this and I think I need to check if there is a simulator and do something.
At this point, do I need to reach the character from the simulator script, capture the coherenceInput and move it according to the value there, if so, how do I do this?
Below I am attaching photos of the code and components including the whole setup.
using Coherence.Toolkit;
using Coherence.Toolkit.Bindings;
using System.Linq;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private CoherenceInput _coherenceInput;
private CoherenceSync _coherenceSync;
private void Awake()
{
_coherenceInput = GetComponent<CoherenceInput>();
_coherenceSync = GetComponent<CoherenceSync>();
Binding positionBinding = _coherenceSync.Bindings.FirstOrDefault(c => c.Name == "position");
positionBinding.predictionMode = Coherence.PredictionMode.InputAuthority;
positionBinding.OnNetworkSampleReceived += DetectMisprediction;
}
private void DetectMisprediction(object sampleData, bool arg2, long arg3)
{
const float MispredictionThreshold = 3;
var networkPosition = (Vector3)sampleData;
var distance = (networkPosition - transform.position).magnitude;
if (distance >= MispredictionThreshold)
{
transform.position = networkPosition;
}
}
private void Update()
{
if (_coherenceSync.HasInputAuthority)
{
SendInputs();
}
}
private void SendInputs()
{
Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
_coherenceInput.SetAxis2D("Joystick",input);
}
private void ProcessInputs()
{
Vector2 input = _coherenceInput.GetAxis2D("Joystick");
transform.Translate(input * Time.deltaTime * 10);
}
}
using Coherence.Toolkit;
using UnityEngine;
public class PlayerMovementSimulator : MonoBehaviour
{
private CoherenceBridge _bridge;
private bool _isSimulator;
private void Awake()
{
_bridge = FindObjectOfType<CoherenceBridge>();
_isSimulator = _bridge.IsSimulatorOrHost;
}
private void Update()
{
if (_isSimulator)
{
}
}
}