I’m trying to add coherence to my simple multiplayer game, but the problem is after joining to the room (rooms work well) unity isn’t recognising the cameras to players, so u can move the player, but you see view of another player camera (you can see yourself from 3rd perspective and you should from first)
Hey there. It’s likely you have the camera setup in the player’s prefab. This means that when you have a replicated player that replicated player also has a camera so if it’s set to be the main camera it will take over. The easy solution to this is using Component Actions.
You can set the camera component to be disabled when replicated (not authority)
Alternatively, you can use the CoherenceSync Events to disable the GameObject that holds the camera and other things you want disabled on the player when it is replicated.
OOH i am having the same issue here, but i wrote code and it got fixed,
Here is the code:
[SerializeField] private Camera playerCamera;
// This field can be set when instantiating or spawning the car to mark it as owned by the local player
public bool isLocalPlayer;
void Start()
{
UpdateCameraOwnership();
}
private void UpdateCameraOwnership()
{
// Enable the camera only if this car is owned by the local player
playerCamera.enabled = isLocalPlayer;
}
and here is where the main code is located:
// Instantiates the selected car prefab at a specified position
private void InstantiateSelectedCar(GameObject carPrefab, bool isLocalPlayer)
{
Vector3 spawnPosition = Vector3.zero; // Adjust as needed for spawn location
Quaternion spawnRotation = Quaternion.identity; // Adjust as needed for initial rotation
GameObject carInstance = Instantiate(carPrefabs[selectedCarIndex], spawnPosition, spawnRotation);
// Set isLocalPlayer for the local player’s car
CarCameraManager cameraManager = carInstance.GetComponent<CarCameraManager>();
if (cameraManager != null)
{
cameraManager.isLocalPlayer = isLocalPlayer;
}
}
Thanks for sharing the code, @muhammadkhankk945!
But check out also @cary’s reply above, because Component Actions are very handy to create behaviour that differs whether that entity is authoritative or not, and you can even create custom ones!
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.