统一光子播放器位置在碰撞时不同步
我正在使用Photon进行多人统一游戏。您可以通过单击空间射击球并使用WSAD移动。当球击中球员时,它会将球员击退。当球击球时,我不会添加任何额外的力量。它是默认物理。问题是,每当player1被player2击中时,player1位置滞后且不能正确同步。
这是演示: https://humbledev.itch.ith.ith.ith.ith.ith.ith.ith.ith.ith.ith.ith-fight-me
具有光子视图,光子变换视图经典和光子刚体视图组件。
PlayerController
private void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
anim = GetComponent<Animator>();
trail = GetComponent<TrailRenderer>();
view = GetComponent<PhotonView>();
}
private void Update()
{
if(view.IsMine){
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
IsJumping = Input.GetKeyDown(KeyCode.Space);
IsShooting = Input.GetMouseButtonDown(0);
IsGrounded = Physics.CheckSphere(groundCheck.position, groundRadius, groundMask);
if(IsJumping && IsGrounded){
rb.AddForce(Vector3.up * jump, ForceMode.Impulse);
}
if(!IsGrounded){
trail.emitting = true;
}else{
trail.emitting = false;
}
if(IsShooting){
view.RPC("RPC_Shoot", RpcTarget.All);
}
}
}
private void FixedUpdate() {
movement = new Vector3(horizontalInput, 0, verticalInput).normalized;
if(movement != Vector3.zero){
anim.SetBool("IsRunning", true);
Quaternion targetRotation = Quaternion.LookRotation(movement);
targetRotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.fixedDeltaTime);
rb.MovePosition(rb.position + movement * speed * Time.fixedDeltaTime);
rb.MoveRotation(targetRotation);
}else{
anim.SetBool("IsRunning", false);
}
if(IsJumping && IsGrounded){
// rb.AddForce(Vector3.up * jump, ForceMode.Impulse);
}
if(rb.velocity.y < 0){
rb.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
}else if(rb.velocity.y > 0 && !IsJumping){
rb.velocity += Vector3.up * Physics.gravity.y * (lowMultiplier - 1) * Time.deltaTime;
}
}
[PunRPC]
void RPC_Shoot(){
GameObject bullet = Instantiate(bulletPrefab, aim.transform.position, Quaternion.identity);
bullet.GetComponent<Rigidbody>().velocity = aim.transform.forward * bulletSpeed;
}
private void OnCollisionEnter(Collision other) {
if(other.gameObject.tag == "Bullet"){
view.RPC("RPC_Hit", RpcTarget.All, other.gameObject);
}
}
[PunRPC]
void RPC_Hit(GameObject item){
Destroy(item);
}
I'm using Photon for multiplayer unity game. You can shoot balls by clicking space and move using wsad. When the ball hits the player it knocks the player back. I'm not adding any extra force when the ball hits the player. Its the default physics. The problem is, whenever player1 gets hit by a ball by player2, player1 position lags and isn't synching properly.
This is the demo: https://humbledev.itch.io/fight-me
Player prefab has a photon view, photon transform view classic and photon rigidbody view components to it.
PlayerController
private void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
anim = GetComponent<Animator>();
trail = GetComponent<TrailRenderer>();
view = GetComponent<PhotonView>();
}
private void Update()
{
if(view.IsMine){
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
IsJumping = Input.GetKeyDown(KeyCode.Space);
IsShooting = Input.GetMouseButtonDown(0);
IsGrounded = Physics.CheckSphere(groundCheck.position, groundRadius, groundMask);
if(IsJumping && IsGrounded){
rb.AddForce(Vector3.up * jump, ForceMode.Impulse);
}
if(!IsGrounded){
trail.emitting = true;
}else{
trail.emitting = false;
}
if(IsShooting){
view.RPC("RPC_Shoot", RpcTarget.All);
}
}
}
private void FixedUpdate() {
movement = new Vector3(horizontalInput, 0, verticalInput).normalized;
if(movement != Vector3.zero){
anim.SetBool("IsRunning", true);
Quaternion targetRotation = Quaternion.LookRotation(movement);
targetRotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.fixedDeltaTime);
rb.MovePosition(rb.position + movement * speed * Time.fixedDeltaTime);
rb.MoveRotation(targetRotation);
}else{
anim.SetBool("IsRunning", false);
}
if(IsJumping && IsGrounded){
// rb.AddForce(Vector3.up * jump, ForceMode.Impulse);
}
if(rb.velocity.y < 0){
rb.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
}else if(rb.velocity.y > 0 && !IsJumping){
rb.velocity += Vector3.up * Physics.gravity.y * (lowMultiplier - 1) * Time.deltaTime;
}
}
[PunRPC]
void RPC_Shoot(){
GameObject bullet = Instantiate(bulletPrefab, aim.transform.position, Quaternion.identity);
bullet.GetComponent<Rigidbody>().velocity = aim.transform.forward * bulletSpeed;
}
private void OnCollisionEnter(Collision other) {
if(other.gameObject.tag == "Bullet"){
view.RPC("RPC_Hit", RpcTarget.All, other.gameObject);
}
}
[PunRPC]
void RPC_Hit(GameObject item){
Destroy(item);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可悲的是,您不能拥有使用重力同步转换的刚性体,您将必须制作僵化的运动运动并使用重力。这样的事情:
Sadly you cannot have a rigidbody that uses gravity sync the transform, you would have to make the rigidbody kinematic and use gravity off. Something Like this: