【问题标题】:Unity 5 Multiplayer collision glitchesUnity 5 多人游戏碰撞故障
【发布时间】:2018-08-11 20:09:50
【问题描述】:

我尝试制作一个简单的多人游戏示例。如果我在 2d 或 3d 中尝试它并不重要,结果是相同的。我将用 3D 来描述它,这是我至少尝试过的。我简单地将两个圆柱体添加到一个场景和一个球中。我为 Player Controller 设置了一个脚本,这样每个 Player 都可以控制自己的 Player。我添加了网络转换,网络 ID 将其设置为 Player 预制的本地播放权限,并完成了我红色的几个教程中描述的所有内容。

认为正在按预期工作。在主机上,我可以与 Ball 和其他 Player 发生碰撞,添加弹跳材料后,当我用力击中他时,球会从 Player 和其他 Player 身上弹开。 (我在点火按钮上添加了一个提升)我没有对碰撞进行编程,我只是让物理引擎完成工作。问题出在客户端,那里的球表现出某种粘性。它不会从播放器中反弹,它会出现故障并且行为方式不正确。

我尝试了碰撞检测 Descrete - Continuous - Continuous Dynmics 的每一种组合,包括插值或不使用或外插。球员和球以及球员/球员之间的组合。

我也尝试了与网络设置的不同组合,但没有任何帮助。

我添加了一些屏幕截图,以便您更好地了解我的尝试和我的设置。

问题是:物理引擎与网络相结合是否能够做我想做的事,或者是更好的方法:对物理进行编程或将播放器从客户端放回服务器并让他远程控制来自客户。我想从键盘捕获输入并将其发送到服务器并在那里进行移动并仅同步回图形。 (我希望你明白我的意思)

玩家变换

玩家刚体

[编辑] 我在球和球员之间的每个组合中尝试了从 5 到 100 的捕捉阈值。刚体在球上看起来相同(质量除外)。球质量为 0,5。 Player 是一个预制件,因此每个 Player 都具有相同的物理特性。 我也玩过游戏物理,尤其是反弹阈值,但没有帮助。

播放器脚本:

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;

public class PlayerController : NetworkBehaviour
{

public float movmentForce = 75;
public float accelerationOnFire;
public GameObject ball;
public float fireRate = 0.5F;
private float nextFire = 0.0F;

private Rigidbody rbPlayer;

// Use this for initialization
void Start()
{
    Vector3 pos;
    Quaternion q;
    pos.x = 0;
    pos.y = 1;
    pos.z = 10;

    transform.position = pos;


    rbPlayer = GetComponent<Rigidbody>();

    ball = GameObject.FindGameObjectWithTag("Ball");


}

// Update is called once per frame
void Update()
{
    if (!isLocalPlayer)
        return;

    float inputHorizontal = CrossPlatformInputManager.GetAxis("Horizontal");
    float inputVertical = CrossPlatformInputManager.GetAxis("Vertical");
    //        float inputHorizontal = Input.GetAxis("Horizontal");
    //        float inputVertical = Input.GetAxis("Vertical");


    if (inputHorizontal != 0)
    {
        rbPlayer.AddForce(Vector3.right * movmentForce * inputHorizontal, ForceMode.Force);
    }
    if (inputVertical != 0)
    {
        rbPlayer.AddForce(Vector3.forward * movmentForce * inputVertical, ForceMode.Force);
    }
    if (CrossPlatformInputManager.GetButton("Fire1") && Time.time > nextFire)
    //        if (Input.GetButton("Fire1") && Time.time > nextFire)
    {
        nextFire = Time.time + fireRate;
        rbPlayer.AddForce(transform.forward * -accelerationOnFire, ForceMode.Force);
    }
    Vector3 pos = transform.position;

    pos.y += 10;
    Camera.main.transform.position = pos;
}

void FixedUpdate()
{
    if (!isLocalPlayer)
        return;

    if (ball)
    {
        Vector3 dir = ball.transform.position - rbPlayer.transform.position;
        float angle = Mathf.Atan2(dir.x, dir.z) * Mathf.Rad2Deg;
        rbPlayer.transform.rotation = Quaternion.AngleAxis(angle, Vector3.up);
        rbPlayer.transform.Rotate(0, 180, 0);
    }
    else
    {
        ball = GameObject.FindGameObjectWithTag("Ball");
    }

}

}

球生成器

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

public class BallSpawn : NetworkBehaviour
{

    public GameObject ballPrefab;


    // Use this for initialization
    public override void OnStartServer()
    {
        SpawnBall();
    }


    void SpawnBall()
    {
        GameObject myBall = Instantiate<GameObject>(ballPrefab);//, Vector2.zero, Quaternion.identity);        

        NetworkServer.Spawn(myBall);

        myBall.tag = "Ball";
    }
}

游戏物理

【问题讨论】:

    标签: networking unity5 collider


    【解决方案1】:

    据我了解,动作要么仅在本地播放(当没有活动的 NetworkTransform 组件时),要么先同步到服务器,然后再同步到其他客户端。所以物理引擎与网络系统没有直接交互,它只改变变换/刚体,然后使用 NetworkTransform 同步。因此,您尝试执行的操作应该有效。

    为了回答您的问题,我会尝试使用球的 NetworkTransform 中的设置。我最近遇到了类似的问题,并通过以下设置解决了它:

    Network Send Rate: 9
    Transfrom Send Mode: Sync Rigidbody 3D
    
    Movement Threshold: 0.001
    Snap Threshold: 100
    Interpolate Movement Factor: 1
    
    Rotation Axis: XYZ (full 3D)
    Interpolate Rotation Factor: 8
    Compress Rotation: None
    Sync Angular Velocity: No
    

    我猜想将 Snap Threshold 设置为更高的值,比如 100,应该可以解决您的问题。

    来自文档:

    如果移动更新使对象远离其当前位置该值,它将捕捉到该位置而不是平滑移动。

    【讨论】:

    • 您好,很抱歉没有解决问题。碰撞保持某种粘性。
    • 我忘了说我说的是你的球上的 NetworkTransform。另外,每个玩家都应该有一个 NetworkTransform,就像您为 Player1 发布的那个一样。然后,当然,它们都需要一个刚体和一个碰撞器,标准设置就足够了。这些设置对我来说效果很好。也许将Edit &gt; Project Settings &gt; Physics 中的全球引力设置为 0 有帮助?
    • 我试过这个没有成功。我更新了我的问题。也许您可以为您的 Player 和球发布刚体和 Networktransform 的设置?谢谢!
    • 我刚刚尝试重建您的项目,现在我确切地知道您所说的粘性行为是什么意思。我尝试了 NetworkTransform 中设置的许多组合,但也无法正常工作,抱歉......但我很确定它应该使用与您的设置类似的设置,也许再试一次。如果我找到解决方案,我会通知您。
    • 好的!感谢您的尝试!如果我找到解决方案,我会发布它;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多