unity如何制作局域网游戏

一、游戏介绍

实现局域网连接的联机游戏,类似于台桌游戏,桌面上有若干方块,当消灭的方块达到一定数量时获胜。

二、游戏实现

1、创建一个空物体,命名为NetworkManager,负责局域网的管理

 2、给NetworkManeger添加两个组件(如下图)

 3、点击运行后可以看到界面中多了几个按钮,第一个按钮是创建客户端,第二个按钮是连接连接服务端。 

 4、用一个plane和四个cube创建一个桌面,并给他们一定的颜色,效果如下:

5、创建若干正方体,把tag改为ball把放在合适的位置

6、创建一个小球,命名为player,tag也修改为player ,并添加组件NetworkIdentity

7、创建脚本move,赋予小球,使小球移动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class move : MonoBehaviour
{
    public float speed = 6.0f;
    public float gravity = -9.8f;
    private CharacterController _charController;
    // Start is called before the first frame update
    void Start()
    {
        _charController = GetComponent<CharacterController>();//使用附加到相同对象上的其他组件
    }
 
    // Update is called once per frame
    void Update()
    {
        float x = Input.GetAxis("Horizontal") * speed;
        float z = Input.GetAxis("Vertical") * speed;
        Vector3 movement = new Vector3(x, 0, z);
        movement = Vector3.ClampMagnitude(movement, speed);//使对角移动的速度和沿轴移动的速度一样
        movement.y = gravity;
        movement *= Time.deltaTime;
        movement = transform.TransformDirection(movement);//本地坐标变为全局坐标
        _charController.Move(movement);//character通过movement向量移动
    }
}

8、新建文件夹prefabs,把player拖到里面作为预制体,然后把场景中的player删除。把player拖到NetworkManager中如下啊位置

9、修改player代码move,使小球碰到方块后方块消失(ball是方块的标签,如果方块的标签没有修改为ball是销毁不了的),给player添加刚体组件(选中prefabs里的小球,选择最右侧的打开预制体,然后添加组件)

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

public class move : MonoBehaviour
{
    public float speed = 100;

    // Start is called before the first frame update
    void Start()
    {
      
    
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        float moveh = Input.GetAxis("Horizontal");
        float movev = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(moveh, 0.0f, movev);
        GetComponent<Rigidbody>().AddForce(movement * speed * Time.deltaTime);//给小球施加力,使小球运动
    }
    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "ball")
        {
            Destroy(other.gameObject);//小球碰到标签为pickup的物体则摧毁物体
         
        }
   
    }
 

}

 10、点击运行,然后点击界面的第一个按钮,即创建客户端,就可以看到小球生成了。

11、导出场景(File——>building settings——>build,选择合适的文件夹),导出后运行,点击场景中的第二个按钮,即连接客户端(如果是在同一台电脑上运行的话,可以直接连接),然后就会出现如下界面,会发现场景中有两个玩家(但是会有bug,画面不同步,两端一起动等等,后面会修改)。

 

 12、解决画面不同步,区分客户端和服务端

1)给player添加两个组件,相关参数设置如下

2)设置player的identify组件

 

 3)改写move代码

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

public class move : NetworkBehaviour
{
    public float speed = 100;

    // Start is called before the first frame update
    void Start()
    {
      
    
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (isLocalPlayer) {//判断当前客户端是不是自己
            playermove();
        }
    }

    public override void OnStartLocalPlayer()//客户端已连接就会调用
    {
        transform.GetComponent<Renderer>().material.color = Color.green;
     //   base.OnStartLocalPlayer();
    }

    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "ball")
        {
            Destroy(other.gameObject);//小球碰到标签为pickup的物体则摧毁物体
         
        }
   
    }
    private void playermove() {
        float moveh = Input.GetAxis("Horizontal");
        float movev = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(moveh, 0.0f, movev);
        GetComponent<Rigidbody>().AddForce(movement * speed * Time.deltaTime);//给小球施加力,使小球运动
    }
 

}

如此便实现局域网连接啦!!!

猜你喜欢

转载自blog.csdn.net/lxy20011125/article/details/130380961
今日推荐