统一光子播放器位置在碰撞时不同步

发布于 2025-01-22 04:47:00 字数 2735 浏览 3 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仲春光 2025-01-29 04:47:00

可悲的是,您不能拥有使用重力同步转换的刚性体,您将必须制作僵化的运动运动并使用重力。这样的事情:

public Rigidbody[] PlayerOwnedRigidbody;

if (photonView.IsMine) return;

foreach (var item in PlayerOwnedRigidbody)
{
    item.isKinematic = true;
    item.useGravity = false;
}

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:

public Rigidbody[] PlayerOwnedRigidbody;

if (photonView.IsMine) return;

foreach (var item in PlayerOwnedRigidbody)
{
    item.isKinematic = true;
    item.useGravity = false;
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文