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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
public class PlayerController : MonoBehaviourPunCallbacks
{
public GameObject Cam;
public CharacterController SelectPlayer;
public float Speed;
public float JumpPow;
private float Gravity;
private Vector3 MoveDir;
private bool JumpButtonPressed;
public ManigerScript maniger;
public bool playing; //게임 실행 판단 변수
public static bool Talking; //대화 실행 판단 변수
public GameObject scanObject;
public PhotonView PV;
void Start()
{
Speed = 5.0f;
Gravity = 10.0f;
MoveDir = Vector3.zero;
JumpPow = 5.0f;
JumpButtonPressed = false;
}
public void Update()
{
playing = ManigerScript.playing;
if(PV.IsMine)
{
if (playing)
{
if (SelectPlayer == null) return;
if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
{
var offset = Cam.transform.forward;
offset.y = 0;
transform.LookAt(SelectPlayer.transform.position + offset);
}
if (SelectPlayer.isGrounded)
{
MoveDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
MoveDir = SelectPlayer.transform.TransformDirection(MoveDir);
MoveDir *= Speed;
if (JumpButtonPressed == false && Input.GetButton("Jump"))
{
//SelectPlayer.transform.rotation = Quaternion.Euler(0, 45, 0);
JumpButtonPressed = true;
Debug.Log("jump");
MoveDir.y = JumpPow;
}
}
else
{
MoveDir.y -= Gravity * Time.deltaTime;
}
if (!Input.GetButton("Jump"))
{
JumpButtonPressed = false;
}
SelectPlayer.Move(MoveDir * Time.deltaTime);
}
}
}
CamController
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
public class CamController : MonoBehaviourPunCallbacks
{
public bool playing;
public GameObject player;
public float xmove = 0;
public float ymove = 0;
public float distance = 3;
public float SmoothTime = 0.1f;
private Vector3 velocity = Vector3.zero;
private int toggleView = 3; // 1=1인칭, 3=3인칭
private float wheelspeed = 8.0f;
public PhotonView PV;
public void Update()
{
playing = ManigerScript.playing;
if (playing)
{
// 마우스 우클릭 중에만 카메라 무빙 적용
if(PV.IsMine)
{
if (toggleView == 3)
{
if (Input.GetMouseButton(1))
{
Cursor.lockState = CursorLockMode.Locked;
xmove += Input.GetAxis("Mouse X");
ymove -= Input.GetAxis("Mouse Y");
}
else
{
Cursor.lockState = CursorLockMode.None;
}
}
else
{
Cursor.lockState = CursorLockMode.Locked;
xmove += Input.GetAxis("Mouse X");
ymove -= Input.GetAxis("Mouse Y");
}
transform.rotation = Quaternion.Euler(ymove, xmove, 0);
if (Input.GetMouseButtonDown(2))
toggleView = 4 - toggleView;
if (toggleView == 3)
{
distance -= Input.GetAxis("Mouse ScrollWheel") * wheelspeed;
if (distance < 1.0f) distance = 1.0f;
if (distance > 100.0f) distance = 100.0f;
}
if (toggleView == 1)
{
Vector3 reverseDistance = new Vector3(0.0f, 0.57f, 0.3f);
transform.position = player.transform.position + transform.rotation * reverseDistance;
}
else if (toggleView == 3)
{
Vector3 reverseDistance = new Vector3(0.0f, 0.0f, distance);
transform.position = Vector3.SmoothDamp(
transform.position,
player.transform.position - transform.rotation * reverseDistance,
ref velocity,
SmoothTime);
}
}
}
}
}