Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Both the players are moving together what is the issue with my code?

Discussion in 'Multiplayer Tools' started by yaswanthkulas, May 3, 2023.

  1. yaswanthkulas

    yaswanthkulas

    Joined:
    Mar 26, 2023
    Posts:
    1
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Photon.Pun;

    public class LinearMovement : MonoBehaviour
    {
    private CharacterController character_Controller;
    private Vector3 move_Direction;

    public float speed = 5f;
    private float gravity = 20f;
    public float jump_Force = 10f;
    private float vertical_Velocity;
    PhotonView PV;
    Rigidbody rb;

    void Awake(){
    character_Controller = GetComponent<CharacterController>();
    rb=GetComponent<Rigidbody>();
    PV = GetComponent<PhotonView>();
    }
    void Start(){
    if (!PV.IsMine){
    Destroy(GetComponentInChildren<Camera>().gameObject);
    Destroy(rb);
    }
    }
    // Update is called once per frame
    void Update(){
    MoveThePlayer();
    if (!PV.IsMine){
    return;
    }
    }

    void MoveThePlayer(){
    move_Direction = new Vector3(Input.GetAxis(Axis.HORIZONTAL), 0f, Input.GetAxis(Axis.VERTICAL));
    move_Direction = transform.TransformDirection(move_Direction);
    move_Direction *= speed * Time.deltaTime;
    ApplyGravity();
    character_Controller.Move(move_Direction);

    PV.RPC("UpdateTransform", RpcTarget.OthersBuffered, transform.position, transform.rotation);

    }

    void ApplyGravity(){
    vertical_Velocity -= gravity * Time.deltaTime;
    if(character_Controller.isGrounded) {
    if(character_Controller.isGrounded && Input.GetKeyDown(KeyCode.Space)) vertical_Velocity = jump_Force;
    }
    move_Direction.y = vertical_Velocity * Time.deltaTime;
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
    if (stream.IsWriting){
    // Send the position and rotation across the network
    stream.SendNext(transform.position);
    stream.SendNext(transform.rotation);
    } else {
    // Receive the position and rotation from the network
    transform.position = (Vector3)stream.ReceiveNext();
    transform.rotation = (Quaternion)stream.ReceiveNext();
    }
    }

    // Define the method to be called across the network
    [PunRPC]
    void UpdateTransform(Vector3 newPosition, Quaternion newRotation){
    transform.position = newPosition;
    transform.rotation = newRotation;
    }
    }
     
  2. CodeNinja-

    CodeNinja-

    Unity Technologies

    Joined:
    Feb 22, 2023
    Posts:
    27
    Hello yaswanthkulas,

    For Photon related questions please use the Photon forum as it is an external tool.
    Anyways, at looking at your code I guess you should apply the movement after checking if the controller matches the local Player:

    Code (CSharp):
    1. void Update()
    2. {
    3.     if (!PV.IsMine)
    4.     {
    5.         return;
    6.     }
    7.     MoveThePlayer();
    8. }
    By the way, when inserting code please use the insert code button for better readability.

    Hope this helps!