我正在制作一个简单的多人游戏。
我有一个网络字符脚本,但只有旋转似乎不起作用。我通过网络收到的角色只是平躺在地上,开始上下颠簸。所有其他的东西(运动,动画师)工作得很好。这是我的NetworkCharacter脚本:
public class NetworkCharacter : Photon.MonoBehaviour
{
private Vector3 correctPlayerPos;
private Quaternion correctPlayerRot;
private Animator anim;
private float correctForward;
private float correctTurn;
private float correctJump;
private float correctJumpLeg;
private bool correctCrouch;
private bool correctOnGround;
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate()
{
if (!photonView.isMine)
{
transform.position = Vector3.Lerp(transform.position, this.correctPlayerPos, Time.deltaTime * 5);
try {
transform.rotation = Quaternion.Lerp(transform.rotation, this.correctPlayerRot, Time.deltaTime * 5);
} catch(Exception e)
{
print(e.Message);
}
anim.SetFloat("Forward", Mathf.Lerp(anim.GetFloat("Forward"), correctForward, Time.deltaTime * 5));
anim.SetFloat("Turn", Mathf.Lerp(anim.GetFloat("Turn"), correctTurn, Time.deltaTime * 5));
anim.SetFloat("Jump", Mathf.Lerp(anim.GetFloat("Jump"), correctJump, Time.deltaTime * 5));
anim.SetFloat("JumpLeg", Mathf.Lerp(anim.GetFloat("JumpLeg"), correctJumpLeg, Time.deltaTime * 5));
anim.SetBool("Crouch", correctCrouch);
anim.SetBool("OnGround", correctOnGround);
}
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
// We own this player: send the others our data
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
stream.SendNext(anim.GetFloat("Forward"));
stream.SendNext(anim.GetFloat("Turn"));
stream.SendNext(anim.GetFloat("Jump"));
stream.SendNext(anim.GetFloat("JumpLeg"));
stream.SendNext(anim.GetBool("Crouch"));
stream.SendNext(anim.GetBool("OnGround"));
}
else
{
// Network player, receive data
this.correctPlayerPos = (Vector3)stream.ReceiveNext();
this.correctPlayerRot = (Quaternion)stream.ReceiveNext();
correctForward = (float)stream.ReceiveNext();
correctTurn = (float)stream.ReceiveNext();
correctJump = (float)stream.ReceiveNext();
correctJumpLeg = (float)stream.ReceiveNext();
correctCrouch = (bool)stream.ReceiveNext();
correctOnGround = (bool)stream.ReceiveNext();
}
}
}
如果需要,我可以尝试制作角色表现的视频。感谢。