1
我正在使用您可以输入的宇宙飞船创建多人游戏。我正在使用光子多人游戏。未同步的多人刚体运动(Photon)
正如你可以在下面的图片中看到,我已经在PhotonView ship的Rigidbody中进行转换,并且在Player上也是如此。
但是,当我进入船舶从其他玩家的看法看起来极端laggy(如果我推船它通常同步)。这是一个显示问题的video。
我的玩家使用固定关节连接到船上。
这里是我的缩短代码:
void Update()
{
//Raycast to check for ship
if ((Physics.Raycast(look, out hit, 9f) && hit.collider.tag == "Ship")
{
//set variables
shipObj = hit.collider.gameObject.transform.parent.gameObject;
ship = shipObj.GetComponent<SmallShipController>();
if (Input.GetKeyDown(KeyCode.R))
{
//check if ship is driven
if (!ship.driven)
{
driving = true;
ship.ChangeDriven();
transform.position = ship.driverSeat.transform.position;
transform.rotation = ship.driverSeat.transform.rotation;
playersCamera.transform.position = ship.thirdPersonPosition.position;
playersCamera.transform.rotation = ship.thirdPersonPosition.rotation;
rb.detectCollisions = false;
rb.mass = 0;
rb.drag = 0;
rb.angularDrag = 0;
photonView.RPC("SetJoint", PhotonTargets.AllBuffered, shipObj.GetPhotonView().viewID);
}
}
if (driving)
{
if (Input.GetKey(KeyCode.A))
{
ship.rb.AddRelativeForce(Vector3.left * ship.moveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.W))
{
ship.rb.AddRelativeForce(Vector3.forward * ship.moveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
ship.rb.AddRelativeForce(Vector3.right * ship.moveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
ship.rb.AddRelativeForce(Vector3.back * ship.moveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.Space))
{
ship.rb.AddRelativeForce(Vector3.up * ship.moveSpeed * Time.deltaTime * 0.5f);
}
if (Input.GetKey(KeyCode.LeftShift))
{
ship.rb.AddRelativeForce(Vector3.down * ship.moveSpeed * Time.deltaTime * 0.5f);
}
if (Input.GetKey(KeyCode.Q))
{
ship.rb.AddRelativeTorque(new Vector3(0, 0, Time.deltaTime * ship.moveSpeed));
}
if (Input.GetKey(KeyCode.E))
{
ship.rb.AddRelativeTorque(new Vector3(0, 0, Time.deltaTime * -ship.moveSpeed));
}
if (Input.GetAxisRaw("Mouse Y") > 0.5f || Input.GetAxisRaw("Mouse Y") < 0.5f || Input.GetAxisRaw("Mouse X") > 0.5f || Input.GetAxisRaw("Mouse X") < 0.5f)
{
ship.rb.AddRelativeTorque(new Vector3(-Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0) * Time.deltaTime * ship.rotationSpeed);
}
if (Input.GetKey(KeyCode.C))
{
ship.rb.velocity = ship.rb.velocity * 0.99f;
ship.rb.angularVelocity = ship.rb.angularVelocity * 0.99f;
}
}
}
}
[PunRPC]
void SetJoint(int shipView)
{
shipObj = PhotonView.Find(shipView).gameObject;
shipObj.GetComponent<FixedJoint>().connectedBody = rb;
}