The other player does not move in multiplayer server
I'm new to this, this is what i see:
As u can see, one is floating and does not move, while the other is not even visible.
Player Script Code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class Player : MonoBehaviourPunCallbacks
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(photonView.IsMine)
{
//local client
float x = Input.GetAxis("Horizontal") * 10f * Time.deltaTime;
float z = Input.GetAxis("Vertical") * 10f * Time.deltaTime;
transform.Translate(x, 0, z);
}
}
}
PlayerMovement Script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class PlayerMove : MonoBehaviourPunCallbacks,IPunObservable
{
public CharacterController controller;
public float speed = 12f;
public float gravity = -9.81f;
public float jumpheight = 3f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
public GameObject playerCamera;
public override void OnEnable()
{
if(photonView.IsMine)
{
controller = GetComponent<CharacterController>();
}
else
{
playerCamera.SetActive(false);
}
}
// Update is called once per frame
void Update()
{
if(photonView.IsMine)
{
isGrounded = Physics.CheckSphere(groundCheck.position,groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if(Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpheight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
throw new System.NotImplementedException();
}
}
Answers
-
I'm sorry you are running into issues. For cases like this, I can only recommend the PUN Basics Tutorial. Read and code along, then compare with your case.
0