Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Indivudual Third Person Camera For Players (Netcode for gameobjects)

Discussion in 'Multiplayer' started by mertshp, Mar 10, 2024.

  1. mertshp

    mertshp

    Joined:
    Mar 16, 2023
    Posts:
    3
    I have been working on single player about 4 years and wanted to improve. Now i started to work on multiplayer with netcode.
    I have a script that i control camera zooming and rotating. Zooming works both for host and client but rotating not working on anyone. I wanted to rotate camera around player when i pressed scrool then move mouse, it will remain constant in other cases. It was working like a charm when its single player but when i try to convert it to multiplayer it got broken. Here is the script I'm talking about.

    Waiting for help, thanks !
    Code (csharp):
    1.  
    2. using Cinemachine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using Unity.Netcode;
    6. using UnityEngine;
    7. public class CameraMovement : MonoBehaviour
    8. {
    9.     [SerializeField] CinemachineVirtualCamera virtualCamera;
    10.     CinemachineComponentBase componentBase;
    11.     float cameraDistance;
    12.     [SerializeField] float sensivity = 10f;
    13.     public Transform PlayerCameraRoot;
    14.     private Vector3 _cameraOffset;
    15.     [Range(0.01f, 100.0f)]
    16.     public float SmoothFactor;
    17.     public bool LookAtPlayer = false;
    18.     public bool RotateAroundPlayer = true;
    19.     public float RotationSpeed = 5f;
    20.     public bool pressedScroolButton;
    21.     void Start()
    22.     {
    23.         _cameraOffset = transform.position - PlayerCameraRoot.position;
    24.      
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         //if(virtualCamera.Follow == null)
    30.         // {
    31.         //     virtualCamera.Follow = GameObject.Find("Player(Clone)").transform;
    32.         // }
    33.         Debug.Log(Input.GetAxis("Mouse X"));
    34.         if(PlayerCameraRoot == null)
    35.         {
    36.         PlayerCameraRoot = GameObject.Find("PlayerCameraRoot").transform;
    37.         }
    38.         if (componentBase == null)
    39.         {
    40.             componentBase = virtualCamera.GetCinemachineComponent(CinemachineCore.Stage.Body);
    41.         }
    42.         if (Input.GetAxis("Mouse ScrollWheel") != 0)
    43.         {
    44.             cameraDistance = Input.GetAxis("Mouse ScrollWheel") * sensivity;
    45.             if (componentBase is CinemachineFramingTransposer)
    46.             {
    47.                 (componentBase as CinemachineFramingTransposer).m_CameraDistance = Mathf.Clamp(((componentBase as CinemachineFramingTransposer).m_CameraDistance), 5, 10);
    48.                 (componentBase as CinemachineFramingTransposer).m_CameraDistance -= cameraDistance;
    49.             }
    50.         }
    51.    
    52.         CheckPressedScrool();
    53.         if (RotateAroundPlayer && pressedScroolButton)
    54.         {
    55.             Quaternion camTurnAngle = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * RotationSpeed, Vector3.up);
    56.             _cameraOffset = camTurnAngle * _cameraOffset;
    57.        
    58.         }
    59.         Vector3 newPos = PlayerCameraRoot.position + _cameraOffset;
    60.      
    61.         transform.position = Vector3.Slerp(transform.position, newPos, SmoothFactor);
    62.         if (LookAtPlayer || RotateAroundPlayer)
    63.         {
    64.             transform.LookAt(PlayerCameraRoot);
    65.         }
    66.     }
    67.     private void CheckPressedScrool()
    68.     {
    69.         if (Input.GetMouseButton(2))
    70.         {
    71.             pressedScroolButton = true;
    72.         }
    73.         else
    74.         {
    75.             pressedScroolButton = false;
    76.         }
    77.     }
    78. }
    79.  
     
    Last edited: Mar 10, 2024
  2. mertshp

    mertshp

    Joined:
    Mar 16, 2023
    Posts:
    3
    There may be lines that are a bit irrelevant to the situation, I'm sorry, I haven't made a complete edit to the script because it's not working yet.
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,974
    Please use code tags for code.

    Your code relies on GameObject.Find("Player(Clone)") which makes the naive assumption that this will be the player's name. This and all other Find assumptions you have to put on the table. Generally, don't use Find. It's inherently brittle and slow. You want to use Inspector references or find by tag or component type.

    Your class derives from MonoBehaviour so it isn't network-aware to begin with. You can't make this work without using NetworkBehviour and doing appropriate checks like if(IsOwner) because some code needs to be owner-specific. If you use the PlayerPrefab reference in NetworkManager then the owner is guaranteed to be the locally controlling player.

    Every player prefab should include its own Camera and Cinemachine virtual camera components. These get enabled only for the IsOwner player to make sure each player is affecting only its own, local camera components.

    Since you already use Cinemachine I'm a little confused why you want to script the rotation with a FramingTransposer. The transposer isn't made for 3rd person, there's an actual 3rd person mode built into Cinemachine virtual cameras. If set up correctly the mouse rotation should just work automatically. You may want to check out Unity's free Third Person Starter Asset in the store for a template.
     
  4. mertshp

    mertshp

    Joined:
    Mar 16, 2023
    Posts:
    3
    • Sorry thats my first time using Unity Forum thanks for the code tag information.
    • When I examined your answer, I realized that my information about the logic of network objects was incomplete or incorrect. So I am looking forward to your answer and explain to me.
    • In my opinion, I did not make cameras network objects because the position or rotation value of the cameras has no effect on the game or other players. So, I thought that making them network objects would be an unnecessary burden for the server.
      But as far as I understand, you are saying i need to turn my cameras to Network Objects ?
    • It seemed strange to me that zoom was working but rotation was not, because everything was working in single player.
    • Clients are taking control of the correct cameras because when I look at the camera following the player, they are on the correct ones. This shows that the GameObject.Find() line is working correctly, the problem should not be there but I understand the logic of your advice, thank you.I have this line inside my ThirdPersonController.cs;
    Code (csharp):
    1.  
    2.  public override void OnNetworkSpawn()
    3.  {
    4.      base.OnNetworkSpawn();
    5.      if(IsClient && IsOwner)
    6.      {
    7.          _cinemachineVirtualCamera.Follow = this.transform;
    8.      }
    9.  }
    10.  
    • This project is already built based on Unity's Third Person Starter Asset.
    • And there is also an interesting situation, a problem that does not exist in single player. As the characters walk around, the camera's position changes suddenly and keeps changing (even if I never press the mouse wheel and try to rotate it). Maybe it will help you pinpoint the source of the problem.
    • I will send a video of the single player version of the game. Everything was working. What I want to adapt to multiplayer is exactly the same.
     
    Last edited: Mar 11, 2024