要用Unity写一个飞机大战,首先需要创建一个飞机和敌人。以下是一个简单的示例:
1. 创建飞机对象:
```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlaneController : MonoBehaviour
{
public float speed = 50f;
public float maxSpeed = 100f;
public float gravity = -9.8f;
public float jumpForce = 200f;
void Update()
{
transform.Translate(Vector3.up * speed * Time.deltaTime);
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
if (horizontalInput != 0 || verticalInput != 0)
{
transform.Translate(Vector3.forward * input * Time.deltaTime);
}
if (Input.GetKeyDown(KeyCode.Space))
{
if (verticalInput > 0)
{
Jump();
}
else if (verticalInput < 0)
{
Land();
}
}
}
void Jump()
{
if (Input.GetButton("Fire1"))
{
Rigidbody2D rb = GetComponent();
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}
}
void Land()
{
Rigidbody2D rb = GetComponent();
rb.velocity = new Vector2(0, 0);
}
}
```
2. 创建敌人对象:
```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController : MonoBehaviour
{
public float speed = 50f;
public float maxSpeed = 100f;
public float gravity = -9.8f;
public float jumpForce = 200f;
void Update()
{
transform.Translate(Vector3.up * speed * Time.deltaTime);
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
if (horizontalInput != 0 || verticalInput != 0)
{
transform.Translate(Vector3.forward * input * Time.deltaTime);
}
if (Input.GetKeyDown(KeyCode.Space))
{
if (verticalInput > 0)
{
Jump();
}
else if (verticalInput < 0)
{
Land();
}
}
}
void Jump()
{
if (Input.GetButton("Fire1"))
{
Rigidbody2D rb = GetComponent();
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}
}
void Land()
{
Rigidbody2D rb = GetComponent();
rb.velocity = new Vector2(0, 0);
}
}
```
3. 创建游戏场景:
```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameScene : MonoBehaviour
{
public GameObject planePrefab; // 飞机预制体
public GameObject enemyPrefab; // 敌人预制体
public int planeCount = 3; // 飞机数量
public int enemyCount = 3; // 敌人数量
private float timer; // 计时器
private bool isPaused = false; // 是否暂停游戏
void Start()
{
timer = 0; // 初始化计时器为0
isPaused = false; // 设置游戏未暂停状态
}
void Update()
{
if (!isPaused) // 如果游戏未暂停,则更新游戏逻辑
{
if (Input.GetKeyDown(KeyCode.Space)) // 如果按下空格键,则开始游戏循环
{
StartCoroutine(GameLoop()); // 启动游戏循环
}
}
}
IEnumerator GameLoop()
{
while (!isPaused) // 游戏循环,直到游戏暂停或结束
{
Update(); // 更新游戏逻辑
timer += Time.deltaTime; // 更新计时器值
yield return null; // 等待一段时间(这里使用yield return null作为占位符)
}
}
}
```
4. 创建主菜单:
```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; // 导入场景管理类库
public class MainMenuController : MonoBehaviour
{
public void PlayGame() // 开始游戏按钮的点击事件处理函数
{
StartCoroutine(LoadLevel()); // 加载关卡1的游戏场景
}
IEnumerator LoadLevel()
{
// 加载关卡1的游戏场景,这里省略了具体的加载代码,你需要根据实际情况编写加载代码
yield return null; // 等待一段时间(这里使用yield return null作为占位符)
SceneManager.LoadScene(1); // 加载关卡1的游戏场景
}
}
```
将以上代码分别放在各自的脚本文件中,并将它们添加到你的项目中。然后,你可以运行游戏并尝试控制飞机和敌人进行战斗。
评论区: