Search Unity

Question Photon multiplay camera

Discussion in 'Game Server Hosting' started by namesd, Oct 14, 2022.

  1. namesd

    namesd

    Joined:
    Aug 24, 2022
    Posts:
    1
    We are making multi-play games using Photon. It's okay when I play single, but when I play multi-play, the camera crosses and becomes weird. Please tell me what to do.

    PlayerController
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5. using Photon.Realtime;
    6.  
    7. public class PlayerController : MonoBehaviourPunCallbacks
    8. {
    9.     public GameObject Cam;
    10.     public CharacterController SelectPlayer;
    11.     public float Speed;
    12.     public float JumpPow;
    13.  
    14.     private float Gravity;
    15.     private Vector3 MoveDir;
    16.     private bool JumpButtonPressed;
    17.  
    18.     public ManigerScript maniger;
    19.     public bool playing; //게임 실행 판단 변수
    20.     public static bool Talking; //대화 실행 판단 변수
    21.  
    22.     public GameObject scanObject;
    23.  
    24.     public PhotonView PV;
    25.  
    26.  
    27.     void Start()
    28.     {
    29.         Speed = 5.0f;
    30.         Gravity = 10.0f;
    31.         MoveDir = Vector3.zero;
    32.         JumpPow = 5.0f;
    33.         JumpButtonPressed = false;
    34.     }
    35.  
    36.  
    37.     public void Update()
    38.     {
    39.         playing = ManigerScript.playing;
    40.  
    41.         if(PV.IsMine)
    42.         {
    43.             if (playing)
    44.             {
    45.                 if (SelectPlayer == null) return;
    46.  
    47.                 if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
    48.                 {
    49.                     var offset = Cam.transform.forward;
    50.                     offset.y = 0;
    51.                     transform.LookAt(SelectPlayer.transform.position + offset);
    52.                 }
    53.  
    54.                 if (SelectPlayer.isGrounded)
    55.                 {
    56.  
    57.                     MoveDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    58.  
    59.                     MoveDir = SelectPlayer.transform.TransformDirection(MoveDir);
    60.  
    61.                     MoveDir *= Speed;
    62.  
    63.  
    64.                     if (JumpButtonPressed == false && Input.GetButton("Jump"))
    65.                     {
    66.                         //SelectPlayer.transform.rotation = Quaternion.Euler(0, 45, 0);
    67.                         JumpButtonPressed = true;
    68.                         Debug.Log("jump");
    69.                         MoveDir.y = JumpPow;
    70.                     }
    71.  
    72.  
    73.                 }
    74.  
    75.                 else
    76.                 {
    77.                     MoveDir.y -= Gravity * Time.deltaTime;
    78.                 }
    79.  
    80.  
    81.                 if (!Input.GetButton("Jump"))
    82.                 {
    83.                     JumpButtonPressed = false;
    84.                 }
    85.  
    86.                 SelectPlayer.Move(MoveDir * Time.deltaTime);
    87.             }
    88.         }
    89.     }
    90.  
    91.    
    92. CamController
    93. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5. using Photon.Realtime;
    6.  
    7. public class CamController : MonoBehaviourPunCallbacks
    8. {
    9. public bool playing;
    10.  
    11. public GameObject player;
    12. public float xmove = 0;
    13. public float ymove = 0;
    14. public float distance = 3;
    15.  
    16.  
    17. public float SmoothTime = 0.1f;
    18.  
    19. private Vector3 velocity = Vector3.zero;
    20.  
    21. private int toggleView = 3; // 1=1인칭, 3=3인칭
    22.  
    23. private float wheelspeed = 8.0f;
    24.  
    25. public PhotonView PV;
    26.  
    27.  
    28. public void Update()
    29. {
    30.         playing = ManigerScript.playing;
    31.  
    32.         if (playing)
    33.         {
    34.             //  마우스 우클릭 중에만 카메라 무빙 적용
    35.             if(PV.IsMine)
    36.             {
    37.                 if (toggleView == 3)
    38.                 {
    39.                     if (Input.GetMouseButton(1))
    40.                     {
    41.                         Cursor.lockState = CursorLockMode.Locked;
    42.                         xmove += Input.GetAxis("Mouse X");
    43.                         ymove -= Input.GetAxis("Mouse Y");
    44.                     }
    45.                     else
    46.                     {
    47.                         Cursor.lockState = CursorLockMode.None;
    48.                     }
    49.                 }
    50.                 else
    51.                 {
    52.                     Cursor.lockState = CursorLockMode.Locked;
    53.                     xmove += Input.GetAxis("Mouse X");
    54.                     ymove -= Input.GetAxis("Mouse Y");
    55.                 }
    56.  
    57.  
    58.                 transform.rotation = Quaternion.Euler(ymove, xmove, 0);
    59.  
    60.                 if (Input.GetMouseButtonDown(2))
    61.                     toggleView = 4 - toggleView;
    62.  
    63.                 if (toggleView == 3)
    64.                 {
    65.                     distance -= Input.GetAxis("Mouse ScrollWheel") * wheelspeed;
    66.                     if (distance < 1.0f) distance = 1.0f;
    67.                     if (distance > 100.0f) distance = 100.0f;
    68.                 }
    69.  
    70.                 if (toggleView == 1)
    71.                 {
    72.                     Vector3 reverseDistance = new Vector3(0.0f, 0.57f, 0.3f);
    73.                     transform.position = player.transform.position + transform.rotation * reverseDistance;
    74.                 }
    75.                 else if (toggleView == 3)
    76.                 {
    77.                     Vector3 reverseDistance = new Vector3(0.0f, 0.0f, distance);
    78.                     transform.position = Vector3.SmoothDamp(
    79.                         transform.position,
    80.                         player.transform.position - transform.rotation * reverseDistance,
    81.                         ref velocity,
    82.                         SmoothTime);
    83.                 }
    84.             }
    85.         }
    86.  
    87.     }
    88. }
     
  2. TomPUnity3D

    TomPUnity3D

    Unity Technologies

    Joined:
    Feb 24, 2022
    Posts:
    2
    Hi there!

    This forum is for the Multiplay product, which hosts your game's dedicated server. It seems like you are having issues using Photon with cameras, is that right?

    If so, I suggest reaching out to Photon support to give you a hand with this. Once you have a working dedicated server and client, you can then upload this dedicated server to Multiplay to get it hosted around the world!

    Best of luck