# Help with players Sync (camera, movement, etc)

**URL:** <https://community.coherence.io/t/help-with-players-sync-camera-movement-etc/700>\
**Category:** Help\
**Created:** [May 10, 2025, 5:54pm UTC](https://community.coherence.io/t/help-with-players-sync-camera-movement-etc/700 "2025-05-10T17:54:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fellype-dev](https://avatars.discourse-cdn.com/v4/letter/f/a5b964/32.png) [@fellype-dev](https://community.coherence.io/u/fellype-dev)\
**Post date:** [May 10, 2025, 5:54pm UTC](https://community.coherence.io/t/help-with-players-sync-camera-movement-etc/700/1 "2025-05-10T17:54:12Z")

</div>

Hi everyone, I’m developing a simple project for college, and I’m having some problems with the synchronization of the players, I followed the 5-minute video that teaches it, but some things didn’t work.

This is the link with a video of the problem: [https://www.youtube.com/watch?v=FBsmmioCAzQ](https://www.youtube.com/watch?v=FBsmmioCAzQ)

Basically, when a player is alone, the camera, flashlight and movement work normally, but when another player enters, everything bugs out.  
I’m a beginner in game development, so this is my first experience with multiplayer.

here are some of the codes i’m using:

**PlayerSpawnHandler.cs (with some different adjustments to the video code)**

```auto
using UnityEngine;
using Coherence.Toolkit;
using Coherence.Connection;

public class PlayerSpawnHandler : MonoBehaviour
{
    [SerializeField] private GameObject playerPrefab;

    private CoherenceBridge _coherenceBridge;
    private GameObject _playerReference;

    private void OnEnable()
    {
        _coherenceBridge = FindObjectOfType<CoherenceBridge>();
        _coherenceBridge.onConnected.AddListener(OnConnected);
        _coherenceBridge.onDisconnected.AddListener(OnDisconnected);
    }

    private void OnDisable()
    {
        if (_coherenceBridge != null)
        {
            _coherenceBridge.onConnected.RemoveListener(OnConnected);
            _coherenceBridge.onDisconnected.RemoveListener(OnDisconnected);
        }
    }

    private void OnDisconnected(CoherenceBridge arg0, ConnectionCloseReason arg1)
    {
        if (_playerReference != null)
            Destroy(_playerReference);
    }

    private void OnConnected(CoherenceBridge arg0)
    {
        _playerReference = Instantiate(playerPrefab, transform.position, transform.rotation);
        
        // Você já trava o cursor dentro do CameraController, mas não faz mal reforçar aqui:
        Cursor.lockState = CursorLockMode.Locked;
    }
}

```

**CameraController.cs:**

```auto
using UnityEngine;

public class CameraController : MonoBehaviour
{
    public float mouseSensitivity = 400.0f;
    public float verticalClamp = 80.0f;
    public float cameraHeight = 2.5f;
    public float smoothFollow = 5f; // Suavização do movimento
    
    private Transform playerBody;
    private CharacterController playerCC;
    private float xRotation = 0f;
    private Vector3 targetPosition;

    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        playerBody = GameObject.FindGameObjectWithTag("Player").transform;
        playerCC = playerBody.GetComponent<CharacterController>();
        
        transform.localPosition = new Vector3(0, cameraHeight, 0);
        transform.localRotation = Quaternion.identity;
    }

    void Update()
    {
        HandleCameraRotation();
    }

    void LateUpdate()
    {
        // Suaviza o acompanhamento do pulo
        targetPosition = playerBody.position + Vector3.up * cameraHeight;
        transform.position = Vector3.Lerp(transform.position, targetPosition, smoothFollow * Time.deltaTime);
    }

    void HandleCameraRotation()
    {
        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -verticalClamp, verticalClamp);

        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        playerBody.Rotate(Vector3.up * mouseX);
    }
}

```

PlayerCharacterController.cs:

```auto
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCharacterController : MonoBehaviour
{
    public float speed = 5f;
    public float runSpeed = 8f; // Velocidade ao correr
    public float gravity = -45.0f;
    public float jumpHeight = 2f;
    private float yVelocity;
    private bool isGrounded;

    public float interactionRadius = 2f;
    public Transform cameraTransform;
    private CharacterController cc;
    public Animator animator;

    void Start()
    {
        cc = GetComponent<CharacterController>();
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    void Update()
    {
        HandleMovement();
        HandleJump();
        HandleCameraRotation();
        UpdateAnimations();
    }

    void HandleMovement()
    {
        isGrounded = cc.isGrounded;

        if (isGrounded && yVelocity < 0)
        {
            yVelocity = -2f;
        }

        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        // Verifica se o Shift está pressionado para correr
        float currentSpeed = Input.GetKey(KeyCode.LeftShift) ? runSpeed : speed;

        Vector3 move = transform.right * horizontal + transform.forward * vertical;
        cc.Move(move * currentSpeed * Time.deltaTime);
    }

    void HandleJump()
    {
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            yVelocity = Mathf.Sqrt(jumpHeight * -2f * gravity);
            if (animator != null)
            {
                animator.SetTrigger("Jump");
            }
        }

        yVelocity += gravity * Time.deltaTime;
        cc.Move(Vector3.up * yVelocity * Time.deltaTime);
    }

    void HandleCameraRotation()
    {
        if (cameraTransform != null)
        {
            cameraTransform.localRotation = Quaternion.Euler(
                cameraTransform.localEulerAngles.x,
                0f,
                0f
            );
        }
    }

    void UpdateAnimations()
    {
        if (animator == null) return;

        Vector2 moveInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
        bool isMoving = moveInput.magnitude > 0.1f;
        animator.SetBool("IsWalking", isMoving);
        animator.SetBool("IsGrounded", isGrounded);
    }

    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.cyan;
        Gizmos.DrawWireSphere(transform.position, interactionRadius);
    }
}

```

---

<div class="post-metadata">

**Author:** ![cary](https://dub1.discourse-cdn.com/flex017/user_avatar/community.coherence.io/cary/32/37_2.png) [@cary](https://community.coherence.io/u/cary)\
**Post date:** [May 10, 2025, 5:58pm UTC](https://community.coherence.io/t/help-with-players-sync-camera-movement-etc/700/2 "2025-05-10T17:58:00Z")

</div>

Hey! It’s very likely that you have the camera and things as part of the prefab that you’re replicating so when they’re created on other clients the camera and other behaviours that you only want on the client with authority are running. See: [2. Prefab setup | coherence Documentation](https://docs.coherence.io/getting-started/setup-a-project/prefab-setup#id-4.-disable-components-on-replicated-objects)

You’ll probably want to disable movement controller and camera things on replicated characters

---

<div class="post-metadata">

**Author:** ![fellype-dev](https://avatars.discourse-cdn.com/v4/letter/f/a5b964/32.png) [@fellype-dev](https://community.coherence.io/u/fellype-dev)\
**Post date:** [May 10, 2025, 6:38pm UTC](https://community.coherence.io/t/help-with-players-sync-camera-movement-etc/700/3 "2025-05-10T18:38:50Z")

</div>

But then the camera and other things need to be outside the player’s prefab? How would these things be spawned when the game starts?

here are the things i disabled

 ![image](https://europe1.discourse-cdn.com/flex017/uploads/coherence/original/1X/67c2ff9ba2aab42fb2cbfb9d9e6b3d74c5c861cb.png)

---

<div class="post-metadata">

**Author:** ![fellype-dev](https://avatars.discourse-cdn.com/v4/letter/f/a5b964/32.png) [@fellype-dev](https://community.coherence.io/u/fellype-dev)\
**Post date:** [May 10, 2025, 6:46pm UTC](https://community.coherence.io/t/help-with-players-sync-camera-movement-etc/700/4 "2025-05-10T18:46:36Z")

</div>

I’m really a little lost

---

<div class="post-metadata">

**Author:** ![cary](https://dub1.discourse-cdn.com/flex017/user_avatar/community.coherence.io/cary/32/37_2.png) [@cary](https://community.coherence.io/u/cary)\
**Post date:** [May 10, 2025, 8:39pm UTC](https://community.coherence.io/t/help-with-players-sync-camera-movement-etc/700/5 "2025-05-10T20:39:27Z")

</div>

So you have two options:

1. create the prefab with all the parts and then disable the parts that only should run on the owner of the prefab when that prefab is replicated on a client (like that doc suggests)
2. create the prefab without the other things and then whatever system is instantiating the prefab would also then instantiate the other things and connect them.

If your plan is to just save your character in the scene then you’ll want to do #1 option, but you’ll find in the longer term that you probably don’t want to save the character in the scene, that you’ll want some kind of character spawn manager so you can control things like where the character spawns. In this case you’ll probably want to switch to option #2.

For option #1 if you want to also disable things not at the root of the prefab hierarchy, you can open the prefab in the editing context and then select the camera and you’ll see the configuration can make changes to that as well:

 ![image](https://europe1.discourse-cdn.com/flex017/uploads/coherence/original/1X/0334130800323c45163f2e64a8431a780e2c603c.png)

---

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex017/uploads/coherence/original/1X/b3aa79eb9519fac7aad7eba18f15a1e8ce60f6e2.png) [@system](https://community.coherence.io/u/system)\
**Post date:** [August 8, 2025, 8:39pm UTC](https://community.coherence.io/t/help-with-players-sync-camera-movement-etc/700/6 "2025-08-08T20:39:42Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
