如何在本地主机和客户端上计算玩家分数

时间:2018-09-11 06:56:50

标签: c# unity3d game-physics

您好,我目前正在制作类似pacman但多人游戏。我在如何联网分数方面遇到麻烦。我想在本地主机上显示自己的分数,并在客户端上显示自己的分数。到目前为止,我只能更新本地主机的分数。谁能帮忙吗?

得分管理器:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;

public class ScoreManager : NetworkBehaviour {


public static int score;        


Text text;                     


void Start()
{

    text = GetComponent<Text>();


    score = 0;
}


void Update()
{
    if (!isServer)
    {
        return;
    }

    text.text = "Score: " + score;
}
}

Ontriggerstart得分

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;




public class PacDot : MonoBehaviour {




void OnTriggerEnter2D(Collider2D co)
{

    if (co.tag == "UFO")
    {
        Destroy(gameObject);
        ScoreManager.score++;
    }

}



}

玩家代码:

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


 public class UFOMove : NetworkBehaviour {

public float speed;


private Rigidbody2D rb2d;  



void Start()
{
    rb2d = GetComponent<Rigidbody2D>();
}

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

    float moveHorizontal = Input.GetAxis("Horizontal");       
    float moveVertical = Input.GetAxis("Vertical");      
    Vector2 movement = new Vector2(moveHorizontal, moveVertical);       
    rb2d.AddForce(movement * speed);
}

}

0 个答案:

没有答案