OnPhotonSerializeView with the rigidbody physics

hi, i'm trying to update the position of my ball (rigidbody) using OnPhotonSerializeView , and drag another gameobject and move it with collision, works good if the photonview is mine but not remotely... hope someone can help me...i copy and paste the code:
using UnityEngine;
using System.Collections;

public class ball : Photon.MonoBehaviour
{
    private Vector3 correctPlayerPos; // We lerp towards this

     // Update is called once per frame
    void FixedUpdate()
    {
        if (!photonView.isMine )
        {
            rigidbody.position = correctPlayerPos;
        }
    }

    void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.isWriting)
        {
            stream.SendNext(rigidbody.position);
        }
        else {
         	
            correctPlayerPos = (Vector3)stream.ReceiveNext();

        }
    }
}

i also tried to use the normal script found on PUN tutorial folder, but the results are the same.
anyone can help me? i also upload i youtube video to show the problem (sorry for the low frame rate but Quicktime decresce a lot the performance )

http://www.youtube.com/watch?v=Iz-8XtJZV5c&feature=youtu.be

Comments

  • You should not only synch position as there are quite some other forces being applied locally as well (rigidbody.velocity and possibly also rigidbody.angularvelocity). Either disable the rigidbody movement for remote players (and only accept their psotions). OR also send the rigidbody.velocity with the latest position so that the simulation is correct.

    So, the easiest solution would be to simply synch the rigidbody.velocity as well.
  • Can u show me how exactly to cast the variable of rigidbody.velocity and rigidbody.angularvelocity? does it needs to be cast or can use the variable that was created already there? And also can u plz show me how to lerp it? is it like Vector3.Lerp(rigidbody.velocity, correctPlayerPos, 0.1f) ; something like this? and same goes to rigidbody.angularvelocity? Here is my code I'm not sure If i did everything correctly or not it works but only problem is it does spawn the object but for a few milliseconds onli and it juz disappear. the object appears to be instantiated in the heirachy tho I don't know why plz check my code out.

    public class RigidbodySmooth : Photon.MonoBehaviour
    {
    Vector3 BulletPosition = Vector3.zero;

    // Update is called once per frame
    void FixedUpdate ()
    {
    rigidbody.position = Vector3.Lerp(rigidbody.position, BulletPosition, 0.1f);
    rigidbody.angularVelocity = Vector3.Lerp(rigidbody.angularVelocity, BulletPosition, 0.1f);
    rigidbody.velocity = Vector3.Lerp(rigidbody.velocity, BulletPosition, 0.1f);
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
    if(stream.isWriting)
    {
    stream.SendNext(rigidbody.position);
    stream.SendNext(rigidbody.velocity);
    stream.SendNext(rigidbody.angularVelocity);

    }
    else
    {
    BulletPosition = (Vector3)stream.ReceiveNext();
    }
    }
    }
  • sorry I forgot to give u my bullet firing script so here goes. I might not send the RPC correctly or something I don't know but the bullet moves very weird in my game. like sometimes it shoots straight but sometimes 3-4 more bullets juz spawn right out of nowhere and the positions are all messed up plz help. Thanks in advance.

    public class FireBullet : Photon.MonoBehaviour
    {

    public string Bullet;
    public Transform Launcher;
    public AudioClip throwSound;
    public float throwForce = 1000f;
    void Update()
    {
    PhotonView Shoot = PhotonView.Get(this);
    Shoot.RPC("Fire", PhotonTargets.All);
    }

    [RPC]
    void Fire ()
    {
    if(Input.GetButtonUp("Fire1"))
    {
    GameObject myBullet = (GameObject)PhotonNetwork.Instantiate(Bullet, Launcher.position, Quaternion.identity, 0);
    myBullet.rigidbody.AddForce(transform.forward * throwForce);
    }
    }
    }
  • You should not instantiate bullets. That's been discussed a lot of times.
    You better send an RPC "i shot from here to there" and on the receiving end, you check "when" this happened. Then you can create the bullet, skip a bit of the path (the piece you lost to lag). If someone gets hit, simply let the bullet owner send some "i hit you" RPC. this also makes sure every player knows who killed who.

    Aside from that, we can't really help you code your game or debug some piece of code. We just don't have the time and each game is different again...