2025年3D游戏设计与编程作业三——沙漠坦克大战

3D游戏设计与编程作业三——沙漠坦克大战这次的作业是制作一个非井字棋的小游戏 但其实目前为止 unity 游戏开发学得非常之少 恐怕无法凭借那么有限的知识制作出一款具有可玩性和娱乐性的小游戏 联想到本课程鼓励借鉴 bushi 于是我干脆从 b 站找到了一个通过 unity3D 制作沙漠坦克大战的教程 于是跟随着 up 主一步步还原了这个小游戏

大家好,我是讯享网,很高兴认识大家。

        这次的作业是制作一个非井字棋的小游戏,但其实目前为止unity游戏开发学得非常之少,恐怕无法凭借那么有限的知识制作出一款具有可玩性和娱乐性的小游戏。

        联想到本课程鼓励借鉴(bushi),于是我干脆从b站找到了一个通过unity3D制作沙漠坦克大战的教程。于是跟随着up主一步步还原了这个小游戏。

        教程传送门: b站_沙漠坦克大战教程

        不过毕竟不是自己开发的游戏,教程什么的我就不一步步的介绍了,只上传一下代码和游戏视频。至于其他的教程视频里讲的很详细了。简单写一下此次作业的收获吧(真不是我懒)。       


讯享网

        完整的跟随着教程做完这个非常之简单的小游戏的时候,收获的东西竟然意外的多。联想到之前有通过Java课设通过JavaFx独立开发过小游戏的经历,这个视频实现了我之前游戏开发过程中想做但做不到的事请——视野跟随。(但毕竟平台不一样,视野跟随这点凭我对JavaFx有限的认知,似乎是办不到的事请。)也通过这次照葫芦画瓢,让我对课上的平移、旋转、缩放等操作有了更深的认识,对unity开发界面更加熟悉了。印象最深刻的还是unity这种开发模式,通过编辑c#脚本并作为“组件”添加到对象的方式,不仅实现了复杂的图像变换,还能够十分方便的实现复用。这么方便的开发方式是我之前用JavaFx开发游戏时想都不敢想的······

        以下是相应脚本的代码(资源文件我就不妨上来了,可以去b站教程领取):

// 坦克移动代码 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Tank_Movement : MonoBehaviour { public float speed = 5; // 移动速度 public float angularSpeed = 5; // 转向速度 private Rigidbody rigidbody1; // 刚体属性 public float number = 1; // 实现不同玩家操控不同坦克,以及设置不同键位 // Start is called before the first frame update void Start() { rigidbody1 = this.GetComponent<Rigidbody>(); // Update is called once per frame } void FixedUpdate() { float v = Input.GetAxis("VerticalP" + number); // 对应上下箭头 rigidbody1.velocity = transform.forward * v * speed; float h = Input.GetAxis("HorizontalP" + number); // 对应左右箭头 rigidbody1.angularVelocity = transform.up * h * angularSpeed; } }

讯享网
讯享网// 坦克开火代码 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Tank_Fire : MonoBehaviour { public GameObject shellPrefab; // 子弹预制体 public KeyCode firekey = KeyCode.Space; // 开火键为空格 public float shellSpeed = 10; // 子弹速度 public Transform firePoint; // 发射点的transform组件 // Start is called before the first frame update void Start() { //firePoint = transform.Find("FirePoint"); // 找到发射点 } // Update is called once per frame void Update() { if(Input.GetKeyDown(firekey)) { // 开火键按下 GameObject go = GameObject.Instantiate(shellPrefab, firePoint.position, firePoint.rotation) as GameObject; go.GetComponent<Rigidbody>().velocity = go.transform.forward * shellSpeed; } } } 
// 子弹爆炸和销毁代码 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Shell_Boom : MonoBehaviour { public GameObject shellExplosionPrefab; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } public void OnTriggerEnter(Collider collider) { GameObject.Instantiate(shellExplosionPrefab, transform.position, transform.rotation); GameObject.Destroy(this.gameObject); if(collider.tag == "Tank") { // 如果碰撞到的物体标签为“Tank”,发送“TankDamage”信息,即调用对象对方的TankDamage函数 collider.SendMessage("TankDamage"); } } } 
讯享网// 子弹爆炸特效销毁代码 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Destroy : MonoBehaviour { public float time = 1.5f; // Start is called before the first frame update void Start() { // 子弹爆炸时间为1.5s,延时销毁 Destroy(this.gameObject, time); } // Update is called once per frame void Update() { } } 
// 坦克血量相关代码 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Tank_Health : MonoBehaviour { public int hp = 100; // 坦克血量 public GameObject tankExplosion; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } void TankDamage() { if (hp <= 0) return; hp -= Random.Range(10, 20); // 随机扣10~20滴血 if(hp <= 0) { GameObject.Instantiate(tankExplosion, transform.position + Vector3.up, transform.rotation); // 如果血量小于等于0,立即播放坦克销毁的特效 GameObject.Destroy(this.gameObject); // 销毁坦克 } } } 
讯享网// 相机视野跟随代码 using System.Collections; using System.Collections.Generic; using UnityEngine; public class FollowTarget : MonoBehaviour { public Transform player1; public Transform player2; private Camera camera; private Vector3 offset; // 坦克中心位置和camera的偏移量 // Start is called before the first frame update void Start() { offset = transform.position - (player1.position + player2.position) / 2; camera = this.GetComponent<Camera>(); // 获得camera对象 } // Update is called once per frame void Update() { if(player1 == null || player2 == null) return; transform.position = (player1.position + player2.position) / 2 + offset; // 调整camera位置 float distance = Vector3.Distance(player1.position, player2.position); // 两坦克之间的距离 float size = distance * 0.875f; // 根据距离缩放相机的size,0.875是缩放比 if (size > 30) size = 30; if (distance <= 8) { camera.orthographicSize = 7; } else camera.orthographicSize = size; } } 

沙漠坦克大战

小讯
上一篇 2025-01-25 13:38
下一篇 2025-03-10 16:35

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/118208.html