Photon RigidBody2D Sync Problems

Options

Hello Unity Forums! I am currently having a lot of trouble with the syncing of Rigidbodies (2D), in my game, and I decided to follow a tutorial, but it seems I can't remove an error. This error says "Cannot implicitly convert type 'UnityEngine.Vector3' to 'float' " on line 48. How would I be able to solve this?


  1. using UnityEngine;
  2. using Photon.Pun;
  3.  
  4. public class PUN2_RigidbodySync : MonoBehaviourPun, IPunObservable
  5. {
  6.  
  7.   Rigidbody2D r;
  8.  
  9.   Vector3 latestPos;
  10.   Quaternion latestRot;
  11.   Vector3 velocity;
  12.   Vector3 angularVelocity;
  13.  
  14.    bool valuesReceived = false;
  15.  
  16.    void Start()
  17.    {
  18.     r = GetComponent<Rigidbody2D>();
  19.    }
  20.  
  21.    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
  22.    {
  23.      if (stream.IsWriting)
  24.      {
  25.       stream.SendNext(transform.position);
  26.       stream.SendNext(transform.rotation);
  27.       stream.SendNext(r.velocity);
  28.       stream.SendNext(r.angularVelocity);
  29.      }
  30.      else
  31.      {
  32.       latestPos = (Vector3)stream.ReceiveNext();
  33.       latestRot = (Quaternion)stream.ReceiveNext();
  34.       velocity = (Vector3)stream.ReceiveNext();
  35.       angularVelocity = (Vector3)stream.ReceiveNext();
  36.  
  37.       valuesReceived = true;
  38.      }
  39.    }
  40.  
  41.    void Update()
  42.    {
  43.      if (!photonView.IsMine && valuesReceived)
  44.      {
  45.       transform.position = Vector3.Lerp(transform.position, latestPos, Time.deltaTime * 5);
  46.       transform.rotation = Quaternion.Lerp(transform.rotation, latestRot, Time.deltaTime * 5);
  47.       r.velocity = velocity;
  48.       r.angularVelocity = angularVelocity;
  49.      }
  50.    }
  51.  
  52.    void OnCollisionEnter2D(Collision2D contact)
  53.    {
  54.      if (!photonView.IsMine)
  55.      {
  56.       Transform collisionObjectRoot = contact.transform.root;
  57.        if (collisionObjectRoot.CompareTag("Player"))
  58.        {
  59.         photonView.TransferOwnership(PhotonNetwork.LocalPlayer);
  60.        }
  61.      }
  62.    }
  63. }


Answers

  • Tobias
    Options

    The question is a simple C# question but the problem is actually this: You can't convert a Vector3 into a single float. There are float values related to Vector3 (e.g. it's length) but ... which one do you need?

    Us Photonians can not help here, sorry.

    Maybe you can take a look at the built-in PhotonRigidbody2DView and figure out what's wrong?