Unity C#简单多人游戏结束

时间:2017-10-22 12:49:26

标签: c# unity3d

如何编辑c#代码以显示其他玩家死亡时玩家1或2获胜。并向死去的玩家展示他们输了?

PlayerController脚本

An exception occurred while executing 'UPDATE server SET name = ? WHERE name = ?' with params ["newservername", "someservername"]:

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`mydb`.`endpoint_server_config`, CONSTRAINT `fk_endpoint_server_config_server` FOREIGN KEY (`server_name`) REFERENCES `server` (`name`) ON DELETE NO ACTION ON UPDATE NO ACTION)

健康脚本

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.Networking;

namespace S3
{
public class PlayerController : NetworkBehaviour {

    public GameObject bulletPrefab;
    public Transform bulletSpawn;

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

    float x = Input.GetAxis ("Horizontal") * Time.deltaTime * 150.0f;
    float z = Input.GetAxis ("Vertical") * Time.deltaTime * 3.0f;

    transform.Rotate (0, x, 0);
    transform.Translate (0, 0, z);

        if (Input.GetKeyDown (KeyCode.Space)) 
        {
            CmdFire();
        }
}

    [Command]
    void CmdFire()
    {
        GameObject bullet = (GameObject) Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);

        bullet.GetComponent<Rigidbody> ().velocity = bullet.transform.forward * 6.0f;

        NetworkServer.Spawn(bullet);

        Destroy (bullet, 2);
    }

    public override void OnStartLocalPlayer()
        {
    GetComponent<MeshRenderer> ().material.color = Color.blue;
    }


}
}

这是codeaspğlasdlişsadlşisadlşiadsilşdslaişdlasişlişdsacdlişsadilşaLDİŞaslişÖDASİÖŞLDÖİLSAŞDöliasödilascöildaiölşsdöiaşsdöilasödilasiöldasöildasöildöilasdöilaöildöildasöildaöilsdöilasdölasdöliasölidasöildsaöildsöiladiösladöilsadöilsadöilasdiölsadiölsaöidlsaöildsöidiösladiölsadiölsadöilasöild

1 个答案:

答案 0 :(得分:0)

您可以使用已获胜的玩家的netID向服务器发送命令,并在netID匹配时检入RPC。
如果netID与本地玩家匹配,那么他就是胜利者。否则,他输了。没有测试下面的代码,但应该工作:

   [Command]
    public void CmdGameWon()
    {
        RpcGameEnd(this.netId);
    }

   [ClientRpc]
    public void RpcGameEnd(NetworkInstanceId nid)
    {
        if(this.isLocalPlayer && this.netId==nid){
            //Process win here
        }else{
            //Process lose here
        }
    }