Hi all, I am currently making a multiplayer fps game and am running into a few issues. While Player rotation seems to be syncing between players, positions aren’t and the player sees the other player as stuck at (0,0,0) despite the position being ticked on the Player’s Photon Transform View Component.
I am loosely following on from this tutorial
.
Thankyou in advance for any suggestions.
using System.Collections;
using System.Collections.Generic;
using Photon.Pun;
using UnityEngine;
using UnityEngine.Audio;
public class PlayerController : MonoBehaviour
{
public float walkSpeed = 20f;
private float sprintFactor;
public float jumpFactor = 5f;
public float gravity = 9.8f;
public Camera Cam;
public float CamSensitivity = 2.5f;
public float XClamp = 45.0f;
private float XMovement;
private float YMovement;
CharacterController cc;
private PhotonView pv;
private Animator animator;
Vector3 direction = Vector3.zero;
float rotationX = 0;
[HideInInspector]
public bool Move = true;
public static bool running;
void Start()
{
cc = GetComponent<CharacterController>();
animator = GetComponent<Animator>();
pv = GetComponent<PhotonView>();
CursorLock();
if (!pv.IsMine)
{
Destroy(GetComponentInChildren<Camera>().gameObject);
}
}
void Update()
{
if (!pv.IsMine) return;
running = Input.GetKey(KeyCode.LeftShift);
sprintFactor = running ? 1.5f : 1f;
XMovement = Move ? Input.GetAxis("Vertical") * sprintFactor * walkSpeed : 0f;
YMovement = Move ? Input.GetAxis("Horizontal") * sprintFactor * walkSpeed : 0f;
float Ydirection = direction.y;
direction = (transform.forward * XMovement) + (transform.right * YMovement);
if (Input.GetButton("Jump") && Move && cc.isGrounded)
{
direction.y = jumpFactor;
animator.SetBool("isJumping", true);
}
else
{
direction.y = Ydirection;
animator.SetBool("isJumping", false);
}
if (!cc.isGrounded)
{
direction.y -= gravity * Time.deltaTime;
}
cc.Move(direction * Time.deltaTime);
if (Move)
{
rotationX += -Input.GetAxis("Mouse Y") * CamSensitivity;
rotationX = Mathf.Clamp(rotationX, -XClamp, XClamp);
Cam.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * CamSensitivity, 0);
}
}
private void CursorLock()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}