英文:
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<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();
}
}
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论