效果预览

https://pan.baidu.com/s/1w3RccGs7FueDopwZTaPrpA#list/path=%2F

资源准备

所需要的资源和上周的AI自动寻路一样: 使用NavMesh实现坦克大战游戏

过程

  1. 创建一个空对象,并命名为NetworkManager, 并添加NetWorkManager组件和NetWorkManagerHUD组件

  2. 给坦克,子弹添加NetWorkIdentity组件和NetWorkTransform组件
    坦克

    子弹

  3. 将子弹和坦克注册到NetworkManager的NetWorkManaer组件中

  4. 制作血条:分别是自己的血条和敌人的血条
    这里还需要给血条加上PlayerHeath和EnemyHealth tag

代码部分

有很大问题的工厂模式和单例模式:(不要问我为什么有问题还要用,因为我懒,不想再写其他的了)
Factory.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Factory : MonoBehaviour {public GameObject bullet;public ParticleSystem bulletPs;public ParticleSystem tankPs;// Use this for initializationprivate Queue<GameObject> bullets = new Queue<GameObject>();private List<ParticleSystem> bulletPses = new List<ParticleSystem>();private List<ParticleSystem> tankPses = new List<ParticleSystem>();void Start () {}// Update is called once per framevoid Update () {}public GameObject GetBullet(){GameObject b = null;Debug.Log(bullets.Count);if (bullets.Count == 0){b = Instantiate<GameObject>(bullet);return b;}b = bullets.Dequeue();return b;}public void RecycleBullet(GameObject b){b.SetActive(false);bullets.Enqueue(b);}public ParticleSystem GetBulletPs(){for (int i = 0; i < bulletPses.Count; ++i){if (!bulletPses[i].isPlaying){return bulletPses[i];}}ParticleSystem p = Instantiate<ParticleSystem>(bulletPs);bulletPses.Add(p);return p;}public ParticleSystem GetTankPs(){for (int i = 0; i < tankPses.Count; ++i){if (!tankPses[i].isPlaying){return tankPses[i];}}ParticleSystem p = Instantiate<ParticleSystem>(tankPs);tankPses.Add(p);return p;}
}

Singleton.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{protected static T instance;public static T Instance{get{if (instance == null){instance = (T)FindObjectOfType(typeof(T));if (instance == null){Debug.LogError("An instance of " + typeof(T) + " is needed in the scene, but there is none.");}}return instance;}}
}

玩家移动和发射子弹代码
Player.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;public class Player : NetworkBehaviour {public float moveSpeed = 10.0f;     //玩家移动速度public float rotateSpeed = 60.0f;   //玩家旋转速度public GameObject BustedTank;private bool isDead = false;public override void OnStartLocalPlayer(){transform.position = new Vector3(Random.Range(0, 10), 0, -5);}void Start () {}// Update is called once per framevoid Update () {if(isDead){return;}if(GetComponent<Health>().health <= 0){ParticleSystem boom = Singleton<Factory>.Instance.GetTankPs();boom.transform.position = transform.position;boom.Play();this.gameObject.SetActive(false);GameObject b = Instantiate<GameObject>(BustedTank);b.transform.position = transform.position;isDead = true;}if (!isLocalPlayer){return;}Camera.main.transform.position = new Vector3(transform.position.x, 15, transform.position.z);if(Input.GetKeyDown(KeyCode.Space)){Cmdfire();}move();}public void move(){float h = Input.GetAxisRaw("Horizontal");   //获取玩家水平轴上的输入float v = Input.GetAxisRaw("Vertical"); //获取玩家在垂直方向的输入transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime * v);//v<0表示获取玩家向后的输入,玩家以moveSpeed的速度向后运动transform.Rotate(Vector3.up * h * rotateSpeed * Time.deltaTime);}[Command]public void Cmdfire(){GameObject b = Singleton<Factory>.Instance.GetBullet();b.transform.position = new Vector3(transform.position.x, 1.5f, transform.position.z) + transform.forward * 1.5f;b.transform.forward = transform.forward;//设置子弹方向Rigidbody rb = b.GetComponent<Rigidbody>();b.GetComponent<Rigidbody>().velocity = transform.forward * 20;//发射子弹NetworkServer.Spawn(b);}
}

玩家生命值代码
Health.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;public class Health : NetworkBehaviour {// Use this for initializationvoid Start () {}// Update is called once per framepublic const int maxHealth = 100;[SyncVar (hook = "onHealthChange")] //检测生命值的代码,用来改变血条public int health = maxHealth;public void TakeDamage(int amount){if(!isServer){return;}health -= amount;}void onHealthChange(int health){//Debug.Log("=====");this.health = health;if(isLocalPlayer){GameObject.FindWithTag("PlayerHealth").GetComponent<Slider>().value = health;}else{GameObject.FindWithTag("EnemyHealth").GetComponent<Slider>().value = health;}}
}

Bullte.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Bullet : MonoBehaviour {public int type; // 0 表示player的子弹, 1 表示enemy的子弹// Use this for initializationpublic int enemyHealth;void Start () {}// Update is called once per framevoid Update () {}void OnCollisionEnter(Collision collision){if (collision.gameObject.tag == "Player"){collision.gameObject.GetComponent<Health>().TakeDamage(10);}ParticleSystem boom = Singleton<Factory>.Instance.GetBulletPs();boom.transform.position = transform.position;boom.Play();if (this.gameObject.activeSelf){Singleton<Factory>.Instance.RecycleBullet(this.gameObject);}}}

其实这个游戏大部分代码都是借鉴潘老师的课件的,而且还有一些Bug未解决

Unity3d实现双人网络坦克大战相关推荐

  1. python实现双人版坦克大战游戏

    游戏介绍: 双人版的<坦克大战>的基本规则是玩家消灭出现的敌方坦克保卫我方基地. 中间还会随机出现很多特殊道具吸收可获得相应的功能,消灭玩即可进入下一关. 方向键:上下左右移动即可.另一个 ...

  2. Unity3d 机试题目 坦克大战总结

    面试的时候,先是正常的面试,然后是笔试,最后是机试.算好一切准备得还算完善,就是机试题目坦克大战,里面会有几个要点老是忘记.在这里首先要郑重感谢我不知道在哪里看到的博客以及他的作者,正好解决了我的疑惑 ...

  3. netty+kotlin实现双人联机坦克大战

    最近因为我在学校报了一个实训项目,这个项目要实现pc安卓双平台所以学习了自己觉得还不错的一个函数语言kotlin,学习过程中有了开发一个联机游戏练手的想法 这里我们实现的方法是将坦克1开启的窗体看作是 ...

  4. Unity3D项目之《坦克大战》

    2016年2月4日11:00:27 Tank_Project_Log 1.新建工程,导入资源,新建Scenes文件夹,保存该场景进该文件夹 2.删除场景中的默认灯光 2.1 再把Prefab里的Lev ...

  5. 双人坦克大战 - Unity3D

    一个简单的双人坦克游戏(3D). GitHub https://github.com/SSGamble/TanksGame 文章目录 总结 创建场景 创建坦克 添加脚本控制坦克移动 双人操作坦克 控制 ...

  6. ava联网3D坦克大战(网络编程)2020

    .游戏效果 Java网络编程联机3D坦克大战 在这里插入图片描述 在这里插入图片描述 二.游戏涉及知识 服务器端运用了 IO.线程.网络.面向对象.异常 的内容, 客户端使用 unity3d引擎进行开 ...

  7. Unity3d 坦克大战开发日志1

    跟着B站上教程开发坦克大战已经有一段时间了,4天了.在此写下关于学习Unity3d引擎时遇到的问题. 1.在制作坦克大战双人对战时,如何进行分屏. 参考博文 如何分屏 总的来说就是,先建立两台Came ...

  8. 期末安卓项目课程设计,Android+spring boot + mybatis-plus的坦克大战双人手机游戏

    一款双人手机游戏<坦克大战>的android项目,可做课程设计作业 简介 这是一个安卓的游戏项目,坦克大战,它是一个双人对战pk的坦克游戏,可以坦克移动,发射子弹,然后有击杀和死亡统计并且 ...

  9. 【跟我一起学Unity3D】做一个2D的90坦克大战之AI系统

    对于AI,我的初始想法非常easy,首先他要能动,而且是在地图里面动. 懂得撞墙后转弯,然后懂得射击,其它的没有了,基于这个想法,我首先创建了一个MyTank类,用于管理玩家的坦克的活动,然后创建AI ...

最新文章

  1. 中国知名企业ERP失败案例深入剖析
  2. Linux服务器上最简单的Nginx反向代理配置
  3. Linux系统主机之间建立信任关系
  4. (个人总结)Linux命令——任意目录查看穿越
  5. 从ThoughtWorks 2017技术雷达看微软技术
  6. flink 复杂事件_复杂的(事件)世界
  7. 【OpenCV 例程200篇】19. 图像的圆形遮罩
  8. 【华为云技术分享】ARM体系结构基础(4)
  9. opencv图像分析与处理(6)- 二维取样定理与二维傅里叶变换
  10. 库存在,编译或运行时提示找不到,要配置LD_LIBRARY_PATH
  11. php 将网页转成pdf_利用PHP将HTML页面转换成PDF文件
  12. 商用密码产品认证-密码机(概述)
  13. 数据统计获取一年 有多少个自然周,起止时间,当前是第几个周
  14. 2021最新的NVIDIA显卡排行榜前十
  15. win11家庭版如何彻底关闭病毒实时保护
  16. SQL基础-操纵表及插入、查询
  17. 005-电脑软件安装包20190408
  18. Linux内核分析及内核编程
  19. 怎么搭建直播平台,直播环境搭建该怎样做?
  20. 中国互联网金融:浪潮还是浪花?

热门文章

  1. 国家著作权: DNA 计算公式, 肽展定理公式与 变嘧啶 推导.
  2. request+python : shuold be true判等的问题
  3. 开发一个标题为Flipflop的游戏应用程序
  4. 字母频率统计 c语言,C语言统计字母使用频率
  5. 2017年4月24号课堂笔记
  6. 国庆七天测(五)马里奥
  7. 笔记之 02_传智播客AJAX视频教程_使用IntelliJ开发Web项目
  8. (转)工商银行U盾全攻略 (兼容Windows 7 多图)
  9. git 解决冲突后提交 fatal: cannot do a partial commit during a merge.
  10. 不看遗憾啊,韩版《大长今》中文音译,天才之作!