如何使用Photon同步拉力娃娃?

huangapple go评论71阅读模式
英文:

how can I sync ragdolls with Using Photon

问题

I'm developing a multiple fps game with Unity using Photon PUN2 and I get a problem with using ragdolls sync player positions on the network player position moving random on every client how can I fix it

我正在使用Unity和Photon PUN2开发一款多人FPS游戏,我遇到了一个关于使用布娃娃同步玩家位置的问题,每个客户端上玩家的位置在网络上随机移动,我该如何修复它?

I enable ragdoll with this code:

我使用以下代码启用了布娃娃:

 public PhotonView pv;
 public Collider headCollider;
 public denemeRotate dr;

 void Start()
 {
     gameObject.AddComponent<SyncRagdoll>();
     gameObject.AddComponent<PhotonRigidbodyView>();
     gameObject.AddComponent<PhotonTransformView>();
     pv.RPC("setRigidbodyState", RpcTarget.All, true);
     pv.RPC("setColliderState", RpcTarget.All, false);
 }

 // Update is called once per frame
 void Update()
 {

 }

 [PunRPC]
 public void Die(Vector3 hitNormal, int currentW)
 {
     Destroy(dr);
     GetComponent<Animator>().enabled = false;
     pv.RPC("setRigidbodyState", RpcTarget.All, false);
     Rigidbody[] rigidbodies = GetComponentsInChildren<Rigidbody>();

     foreach (Rigidbody rigidbody in rigidbodies)
     {     
         if(currentW == 5)
         {
             rigidbody.AddForce(-hitNormal * 200f, ForceMode.Impulse);
         }
         else
         {
             rigidbody.AddForce(-hitNormal * 100f, ForceMode.Impulse);
         }      
     }
     pv.RPC("setColliderState", RpcTarget.All, true);
 }

 [PunRPC]
 void setRigidbodyState(bool state)
 {
     Rigidbody[] rigidbodies = GetComponentsInChildren<Rigidbody>();

     foreach (Rigidbody rigidbody in rigidbodies)
     {
         rigidbody.isKinematic = state;
     }
 }

 [PunRPC]
 void setColliderState(bool state)
 {
     Collider[] colliders = GetComponentsInChildren<Collider>();

     foreach (Collider collider in colliders)
     {
         if(collider != headCollider)
         {
             collider.enabled = state;
         }
     }
 }

I try this code to sync ragdolls but didn't work

我尝试使用以下代码同步布娃娃,但没有成功:

 public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
 {
     if (stream.IsWriting)
     {
         stream.SendNext(transform.position);
         stream.SendNext(transform.rotation);
         stream.SendNext(GetComponent<Rigidbody>().velocity);
         stream.SendNext(GetComponent<Rigidbody>().angularVelocity);
         stream.SendNext(GetComponent<Rigidbody>().drag);
         stream.SendNext(GetComponent<Rigidbody>().angularDrag);
         stream.SendNext(GetComponent<Rigidbody>().mass);
     }
     else
     {
         Vector3 syncPosition = (Vector3)stream.ReceiveNext();
         Quaternion syncRotation = (Quaternion)stream.ReceiveNext();
         Vector3 syncVelocity = (Vector3)stream.ReceiveNext();
         Vector3 syncAngularVelocity = (Vector3)stream.ReceiveNext();

         transform.position = syncPosition;
         transform.rotation = syncRotation;
         GetComponent<Rigidbody>().velocity = syncVelocity;
         GetComponent<Rigidbody>().angularVelocity = syncAngularVelocity;
         GetComponent<Rigidbody>().drag = (float)stream.ReceiveNext();
         GetComponent<Rigidbody>().angularDrag = (float)stream.ReceiveNext();
         GetComponent<Rigidbody>().mass = (float)stream.ReceiveNext();
     }
 }
英文:

I'm developing a multiple fps game with Unity using Photon PUN2 and I get a problem with using ragdolls sync
player positions on the network player position moving random on every client how can I fix it

I enable ragdoll with this code:

 public PhotonView pv;

    public Collider headCollider;

    public denemeRotate dr;

    // Start is called before the first frame update
    void Start()
    {
        gameObject.AddComponent&lt;SyncRagdoll&gt;();
        gameObject.AddComponent&lt;PhotonRigidbodyView&gt;();
        gameObject.AddComponent&lt;PhotonTransformView&gt;();
        pv.RPC(&quot;setRigidbodyState&quot;,RpcTarget.All,true);
        pv.RPC(&quot;setColliderState&quot;, RpcTarget.All,false);
    }

    // Update is called once per frame
    void Update()
    {
        
    }


    [PunRPC]
    public void Die(Vector3 hitNormal,int currentW)
    {
        Destroy(dr);
        GetComponent&lt;Animator&gt;().enabled = false;
        pv.RPC(&quot;setRigidbodyState&quot;, RpcTarget.All, false);
        Rigidbody[] rigidbodies = GetComponentsInChildren&lt;Rigidbody&gt;();

        foreach (Rigidbody rigidbody in rigidbodies)
        {     
            if(currentW == 5)
            {
                rigidbody.AddForce(-hitNormal * 200f, ForceMode.Impulse);
            }
            else
            {
                rigidbody.AddForce(-hitNormal * 100f, ForceMode.Impulse);
            }      
        }
        pv.RPC(&quot;setColliderState&quot;, RpcTarget.All, true);
    }

    [PunRPC]
    void setRigidbodyState(bool state)
    {
        Rigidbody[] rigidbodies = GetComponentsInChildren&lt;Rigidbody&gt;();

        foreach (Rigidbody rigidbody in rigidbodies)
        {
            rigidbody.isKinematic = state;
        }

     
    }

    [PunRPC]
    void setColliderState(bool state)
    {
        Collider[] colliders = GetComponentsInChildren&lt;Collider&gt;();

        foreach (Collider collider in colliders)
        {
            if(collider != headCollider)
            {
                collider.enabled = state;
            }
         
        }

        

    }

I try this code to sync ragdolls but didn't work

 public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            stream.SendNext(transform.position);
            stream.SendNext(transform.rotation);
            stream.SendNext(GetComponent&lt;Rigidbody&gt;().velocity);
            stream.SendNext(GetComponent&lt;Rigidbody&gt;().angularVelocity);
            stream.SendNext(GetComponent&lt;Rigidbody&gt;().drag);
            stream.SendNext(GetComponent&lt;Rigidbody&gt;().angularDrag);
            stream.SendNext(GetComponent&lt;Rigidbody&gt;().mass);
        }
        else
        {
            Vector3 syncPosition = (Vector3)stream.ReceiveNext();
            Quaternion syncRotation = (Quaternion)stream.ReceiveNext();
            Vector3 syncVelocity = (Vector3)stream.ReceiveNext();
            Vector3 syncAngularVelocity = (Vector3)stream.ReceiveNext();

            transform.position = syncPosition;
            transform.rotation = syncRotation;
            GetComponent&lt;Rigidbody&gt;().velocity = syncVelocity;
            GetComponent&lt;Rigidbody&gt;().angularVelocity = syncAngularVelocity;
            GetComponent&lt;Rigidbody&gt;().drag = (float)stream.ReceiveNext();
            GetComponent&lt;Rigidbody&gt;().angularDrag = (float)stream.ReceiveNext();
            GetComponent&lt;Rigidbody&gt;().mass = (float)stream.ReceiveNext();
        }
    }

答案1

得分: 1

创建一个布尔值 Activate Ragdoll,并通过流共享其状态,就像你正在做的那样。当接收时,在更新时检查 IF,如果为真,则禁用 Animator 组件。

英文:

create a boolean Activate Ragdoll, and share its state via stream as you are doing.
When receiving, check the IF on Update, if it is true, then disable the Animator Component

huangapple
  • 本文由 发表于 2023年5月6日 19:44:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定