Unity游戏源码分享-射击游戏Low Poly FPS Pack 3.2
发布人:shili8
发布时间:2025-01-13 05:22
阅读次数:0
**Unity游戏源码分享**
**射击游戏Low Poly FPS Pack3.2**
在本文中,我们将分享一个完整的Unity游戏源码,包括低保真风格的FPS游戏包Low Poly FPS Pack3.2。这个包包含了所有必要的脚本、预制体和资源,以便您可以轻松地创建一个类似于《CS:GO》或《Doom》等经典射击游戏。
**项目结构**
markdownLowPolyFPSPack/ Assets/ Resources/ Prefabs/ Scripts/ Gameplay/ UI/ Util/ ...
**脚本概览**
* **PlayerController.cs**:控制玩家移动、跳跃和攻击的脚本。
* **EnemyController.cs**:控制敌人移动和攻击的脚本。
* **BulletController.cs**:控制子弹飞行和碰撞的脚本。
* **HealthSystem.cs**:管理玩家和敌人的生命值的脚本。
* **ScoreManager.cs**:管理游戏分数的脚本。
**PlayerController.cs**
csharpusing UnityEngine; public class PlayerController : MonoBehaviour{ // 移动速度 public float moveSpeed =5f; // 跳跃力度 public float jumpForce =10f; // 攻击间隔 public float attackInterval =0.5f; private Rigidbody rb; void Start() { rb = GetComponent(); } void Update() { // 移动 float horizontalInput = Input.GetAxis("Horizontal"); float verticalInput = Input.GetAxis("Vertical"); Vector3 movement = new Vector3(horizontalInput,0f, verticalInput); rb.AddForce(movement * moveSpeed); // 跳跃 if (Input.GetButtonDown("Jump") && IsGrounded()) { rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse); } // 攻击 if (Input.GetButton("Fire1") && Time.time - lastAttackTime >= attackInterval) { Shoot(); lastAttackTime = Time.time; } } bool IsGrounded() { return Physics.Raycast(transform.position, Vector3.down,0.1f); } void Shoot() { // 创建子弹 GameObject bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity) as GameObject; // 设置子弹速度和方向 BulletController bc = bullet.GetComponent (); bc.speed =10f; bc.direction = (transform.forward + Vector3.up *0.5f).normalized; } private float lastAttackTime; }
**EnemyController.cs**
csharpusing UnityEngine; public class EnemyController : MonoBehaviour{ // 移动速度 public float moveSpeed =2f; // 攻击间隔 public float attackInterval =1f; private Rigidbody rb; void Start() { rb = GetComponent(); } void Update() { // 移动 Vector3 movement = new Vector3(Random.Range(-1f,1f),0f, Random.Range(-1f,1f)); rb.AddForce(movement * moveSpeed); // 攻击 if (Time.time - lastAttackTime >= attackInterval) { Attack(); lastAttackTime = Time.time; } } void Attack() { // 创建子弹 GameObject bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity) as GameObject; // 设置子弹速度和方向 BulletController bc = bullet.GetComponent (); bc.speed =5f; bc.direction = (transform.forward + Vector3.up *0.5f).normalized; } private float lastAttackTime; }
**BulletController.cs**
csharpusing UnityEngine; public class BulletController : MonoBehaviour{ // 子弹速度 public float speed =10f; // 子弹方向 public Vector3 direction; void Update() { transform.position += direction * speed * Time.deltaTime; if (IsOutofBounds()) { Destroy(gameObject); } } bool IsOutofBounds() { return transform.position.x < -10f || transform.position.x >10f || transform.position.z < -10f || transform.position.z >10f; } }
**HealthSystem.cs**
csharpusing UnityEngine; public class HealthSystem : MonoBehaviour{ // 玩家生命值 public int playerHealth =100; // 敌人生命值 public int enemyHealth =50; void Update() { if (playerHealth <=0) { GameOver(); } if (enemyHealth <=0) { WinGame(); } } void TakeDamage(int damage) { playerHealth -= damage; } void EnemyTakeDamage(int damage) { enemyHealth -= damage; } void GameOver() { // 游戏结束逻辑 } void WinGame() { // 游戏胜利逻辑 } }
**ScoreManager.cs**
csharpusing UnityEngine; public class ScoreManager : MonoBehaviour{ // 玩家分数 public int playerScore =0; // 敌人分数 public int enemyScore =0; void Update() { if (playerHealth >0) { playerScore++; } if (enemyHealth >0) { enemyScore++; } } }
以上是Low Poly FPS Pack3.2的源码分享,包括了所有必要的脚本、预制体和资源。您可以根据自己的需求进行修改和扩展,以创建一个更完整的游戏体验。