【坦克大战】Unity3D多人在线游戏(泰课的坦克大战--旋转的螺丝钉)
【坦克大战】Unity3D多人在线游戏
http://www.taikr.com/my/course/937

1.NetworkManager的介绍:




说明:选择固定生成时会自动寻找有StartPosition组件的位置
2.NetWorkDiscovery组件的介绍:
使用在局域网中的一个组件,在英特网上不能使用
官方文档:




说明:NetWorkDiscovery与Network managerHUD相似:
Network managerHUD介绍:就是显示Network manager的,如下图:


3.框架部分,开始界面的5个按钮

IndexUI
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IndexUI : MonoBehaviour {
public void SingleBtn()
{
}
public void MutiplayBtn()
{
}
public void LanBtn()
{
}
}
4.LanGame局域网对战功能的实现

放在NetWorkManagerCustom中的代码:
public static void LanGame()
{
//转换一下再调用
singleton.StartCoroutine((singleton as NetWorkManagerCustom).DiscoveryNetWork());
}
public IEnumerator DiscoveryNetWork()
{
//取得Discovery组件
NetworkDiscoverCustom discovery = GetComponent<NetworkDiscoverCustom>();
discovery.Initialize();//组件初始化
discovery.StartAsClient();//扫描局域网的服务器
yield return new WaitForSeconds(2);
//没有找到局域网中的服务器的话就建立服务器
if (discovery.running)
{
discovery.StopBroadcast();//停掉广播包
yield return new WaitForSeconds(0.5f);
discovery.StartAsServer();//作为服务器发射广播包
StartHost();//作为服务器和客户端同时启动
//StartClient();//作为客户端启动
//StartServer();//只作为服务器启动
}
}
放在NetworkDiscoverCustom中的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class NetworkDiscoverCustom : NetworkDiscovery {
public override void OnReceivedBroadcast(string fromAddress,string data)
{
StopBroadcast();
NetWorkManagerCustom.singleton.networkAddress = fromAddress;
NetWorkManagerCustom.singleton.StartClient();
}
}
5.NetGame,英特网的在线对战:

public static void NetGame()
{
singleton.StartMatchMaker();//表示启用Unet网络对战功能
singleton.matchMaker.ListMatches(0, 20, "", false, 0, 0, singleton.OnMatchList);
//第1个参数:startpagenumber:表示第几页的list
//第2个参数:表示每一页有多少个
//第3个参数:表示需要找到的房间名称
//第4个参数:表示是否返回带有密钥的房间
//第5个参数:表示的是竞赛方面的设置
//第6个参数:表示的是一个域,只能从这个域上面返回房间
//第7个参数:是一个回调的函数
}
public override void OnMatchList(bool success, string extendedInfo, List<MatchInfoSnapshot> matchList)
{
if (!success) return;
if (matchList != null)
{
List<MatchInfoSnapshot> availableMatches = new List<MatchInfoSnapshot>();
foreach (MatchInfoSnapshot match in matchList)
{
if (match.currentSize < match.maxSize)
{
availableMatches.Add(match); //保存房间玩家没有满的情况
}
}
//列表的数量是否为0,为0创建服务器,不为0的话就加入服务器
if (availableMatches.Count == 0)
{
//创建服务器
CreateMatch();
}
else
{
//加入服务器
matchMaker.JoinMatch(availableMatches[Random.Range(0, availableMatches.Count - 1)].networkId, "", "", "", 0, 0, OnMatchJoined);
}
}
}
void CreateMatch() //告诉Unet创建网络服务器
{
matchMaker.CreateMatch("", matchSize, true, "", "", "", 0, 0, OnMatchCreate);
//第1个参数:房间名称
//第2个参数:房间可玩家数
//第3个参数:
//第4个参数:口令
//第5个参数:Client Ip地址(公网)
//第6个参数:私网地址
}
public override void OnMatchCreate(bool success, string extendedInfo, MatchInfo matchInfo)
{
if (!success) return;
StartHost(matchInfo);//利用Unet返回的matchinfo创建服务器
}
public override void OnMatchJoined(bool success, string extendedInfo, MatchInfo matchInfo)
{
if (!success)
{
int currentScene = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentScene);
return;
}
StartClient(matchInfo); //利用Unet传回的matchinfo启动客户端
}
6.单人模式:
public static void SimpleGame()
{
singleton.StartHost(singleton.connectionConfig, 1);
}
7.NetWorkTransform


8、角色的移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Player : NetworkBehaviour {
private Rigidbody rb;
public float MoveSpeed = 8f;
void Awake () {
rb = transform.GetComponent<Rigidbody>();
}
void FixedUpdate () {
if (!isLocalPlayer) return;//保证只对本地角色进行控制
Vector2 moveDir;
if (Input.GetAxisRaw("Horizontal") == 0 && Input.GetAxisRaw("Vertical") == 0)
{
moveDir.x = 0;
moveDir.y = 0;
}
else
{
//可以移动
moveDir.x = Input.GetAxis("Horizontal");
moveDir.y = Input.GetAxis("Vertical");
Move(moveDir);
}
}
private void Move(Vector2 direction=default(Vector2))
{
if (direction != Vector2.zero)
{
//转方向
transform.rotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.y));
//计算前方的点
Vector3 movementDir = transform.forward * MoveSpeed * Time.deltaTime;
//移动到这个点
rb.MovePosition(rb.position + movementDir);
}
}
}
9.摄像机跟随
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour {
public Transform target;
public float distance = 10f;
public float height = 5f;
void Update()
{
if (!target) return;
Quaternion currentRotation = Quaternion.Euler(0, transform.eulerAngles.y, 0);
Vector3 pos = target.position;//target的坐标
pos -= currentRotation * Vector3.forward * Mathf.Abs(distance);//距离
pos.y = target.position.y + Mathf.Abs(height);//高度
transform.position = pos;
transform.LookAt(target);
//下面这一句其实要不要无所谓,只是为了精确
transform.position = target.position - (transform.forward * Mathf.Abs(distance));
}
}
在player中重写生成本地物体时调用的方法,把坦克设置为摄像机的target
//把摄像机的target用代码设置为坦克的--这里挺重要的,在OnStartLocalPlayer()方法里面写
public override void OnStartLocalPlayer()
{
Camera.main.GetComponent<CameraFollow>().target = transform;
}
10.炮台的转动及炮台转动的同步

[HideInInspector] //在面板上隐藏公有值
[SyncVar(hook = "OnTurretRotation")]//turretRotation同步到所有客户端,并调用OnTurretRotation()方法
public int turretRotation;
l
//把输入的屏幕坐标转为ray射线
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Plane plane = new Plane(Vector3.up, Vector3.up);//虚构的平面
float distance = 0f;//屏幕宽度
Vector3 hitPos = Vector3.zero;//设置打击点,取默认值
if(plane.Raycast(ray,out distance))
{
hitPos = ray.GetPoint(distance) - transform.position;
}
RotateTurret(new Vector2(hitPos.x, hitPos.z));

/// <summary>
/// 炮台旋转
/// </summary>
/// <param name="direction"></param>
void RotateTurret(Vector2 direction = default(Vector2))
{
if (direction == Vector2.zero) return;
int newRotation = (int)(Quaternion.LookRotation(new Vector3(direction.x, 0, direction.y)).eulerAngles.y);
turret.rotation = Quaternion.Euler(0, newRotation, 0);
turretRotation = newRotation;
}
[Command]
void CmdRotateTurret(int value)
{
turretRotation = value;//调用服务器这个值得改变
}
void OnTurretRotation(int value)
{
if (isLocalPlayer) return;
turretRotation = value;
turret.rotation = Quaternion.Euler(0, turretRotation, 0);//进行旋转
}
11.鼠标右键转向
【坦克大战】Unity3D多人在线游戏(泰课的坦克大战--旋转的螺丝钉)的更多相关文章
- Unity3d多人在线教程
[转载]Unity3d多人在线教程 (2013-02-25 16:02:49) 转载▼ 标签: 转载 原文地址:Unity3d多人在线教程作者:lsy0031 Unity 多个玩家开发教程 Uni ...
- 一个3D的多人在线游戏, 服务端 + 客户端 【转】
最近学院组织了一个实训,要求是利用Socket通信和D3D的知识, 写一个多人在线的游戏, 服务端是在linux下, 客户是在Windows下: 写这个的目的是想让大家给我找错, 欢迎大家的意见.我的 ...
- 试玩 GOWOG ,初探 OpenAI(使用 NeuroEvolution 神经进化)与 Golang 多人在线游戏开发
GOWOG: 原项目:https://github.com/giongto35/gowog 我调整过的:https://github.com/Kirk-Wang/gowog GOWOG 是一款迷你的, ...
- GJM :多人在线游戏的设计思路
感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...
- 负载均衡--大型在线系统实现的关键(上篇)(再谈QQ游戏百万人在线的技术实现)
http://blog.csdn.net/sodme/article/details/393165 —————————————————————————————————————————————— 本文作 ...
- Golang+Protobuf+PixieJS 开发 Web 多人在线射击游戏(原创翻译)
简介 Superstellar 是一款开源的多人 Web 太空游戏,非常适合入门 Golang 游戏服务器开发. 规则很简单:摧毁移动的物体,不要被其他玩家和小行星杀死.你拥有两种资源 - 生命值(h ...
- cocos2d-x游戏开发系列教程-坦克大战游戏之虚拟手柄控制坦克移动
上篇显示了控制手柄,但是还不能用来控制坦克, 这篇将会讲手柄和坦克的移动结合起来. 1.先在CityScene场景中实现场景的虚函数virtual void onEnter(); onEnter在进入 ...
- cocos2d-x游戏开发系列教程-坦克大战游戏之敌方坦克AI的编写
在上篇我们完成了子弹和地图碰撞的检测,在这篇我们将完成敌方坦克AI的编写. 具体思路是屏幕中保持有四个敌方坦克,然后坦克随机方向运动,并且子弹消失后1秒发射一次 1.我们新建一个敌方坦克的AI类来控制 ...
- [Unity3D入门]分享一个自制的入门级游戏项目"坦克狙击手"
[Unity3D入门]分享一个自制的入门级游戏项目"坦克狙击手" 我在学Unity3D,TankSniper(坦克狙击手)这个项目是用来练手的.游戏玩法来自这里(http://ww ...
随机推荐
- float浮动-清浮动BFC渲染机制
float浮动,用于横向布局. 起初的横向布局都用display:inline-block,但是这会导致两个元素之间有空隙,而这是由代码换行解析成空格的,解决元素间有空隙,空格:font-size:0 ...
- 【20190305】CSS-响应式图片:srcset+sizes,picture,svg
响应式图片可以根据不同的设备屏幕大小从而选择加载不同的图片,从而节省带宽.实现响应式图片有三种方法:srcset+sizes属性.picture标签.svg 1. srcset+sizes srcse ...
- 【代码笔记】Web-JavaScript-JavaScript JSON
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- iOS ----------NSDate 、CFAbsoluteTimeGetCurrent、CACurrentMediaTime 的区别
框架层: NSDate 属于Foundation CFAbsoluteTimeGetCurrent() 属于 CoreFoundatio CACurrentMediaTime() 属于 QuartzC ...
- 使用docker部署skywalking
使用docker部署skywalking Intro 之前在本地搭建过一次 skywalking + elasticsearch ,但是想要迁移到别的机器上使用就很麻烦了,于是 docker 就成了很 ...
- git submodule 删除及更新URL 转载的
删除一个submodule 1.删除 .gitsubmodule中对应submodule的条目 2.删除 .git/config 中对应submodule的条目 3.执行 git rm --cache ...
- MongoDB 3.6版本关于bind_ip设置
2017年下半年新发布的MongoDB 3.6版本在安全性上做了很大提升,主要归结为两点: 1.将将bind_ip 默认值修改为了localhost: 2. 在db.createUser()和 db. ...
- 用 Heapster 监控集群 - 每天5分钟玩转 Docker 容器技术(176)
Heapster 是 Kubernetes 原生的集群监控方案.Heapster 以 Pod 的形式运行,它会自动发现集群节点.从节点上的 Kubelet 获取监控数据.Kubelet 则是从节点上的 ...
- AngularJS学习之旅—AngularJS Table(十一)
1.AngularJS 表格 1. ng-repeat 指令可以完美的显示表格 AngularJS 实例 <!DOCTYPE html> <html> <head> ...
- PHP生成PDF并转换成图片爬过的坑
需求描述:根据订单通过模板合同生成新的PDF合同通过e签宝签约后转为图片给用户下载. 需求整理: 1.如何生成PDF文件:使用TCPDF扩展生成.思考: ⑴为了方便将模板中的固定占位符替换为订单中的内 ...