实现效果
1.敌人方实现
敌人代码
using UnityEngine;
using UnityEngine.UI;
public class EnemyBasics : MonoBehaviour
{
public int EnemySpeed { get; internal set; }
public int EnemyAttackDistance { get; internal set; }
public int EnemyChaseDistance { get; internal set; }
/* //初级怪物,可以慢速追击
public abstract void SlowEnemy(GameObject player, float distance, Text gameOverText, Image gameOverImage);
//中级怪物。可以快速追击
public abstract void MiddelEnemy(GameObject player, float distance, Text gameOverText, Image gameOverImage);
//高级怪物,可以发射子弹
public abstract void HighEnemy(GameObject player, float distance, Text gameOverText, Image gameOverImage);*/
public void UpdateEnemy(GameObject player, float distance, Text gameOverText, Image gameOverImage)
{
//如果距离小于10cmEnemyChaseDistance,追踪玩家角色
if (distance < EnemyChaseDistance)
{
transform.LookAt(player.transform);
//向玩家角色移动
transform.Translate(Vector3.forward * EnemySpeed * Time.deltaTime);
}
//如果距离玩家角色小于2cmEnemyAttackDistance
if (distance < EnemyAttackDistance)
{
gameOverText.text = "玩家角色死亡,游戏结束";
gameOverImage.gameObject.SetActive(true);
Time.timeScale = 0;
}
}
}
using UnityEngine;
using UnityEngine.UI;
public class EnemyFactory : MonoBehaviour
{
[SerializeField] private GameObject slowEnemyPrefab;
[SerializeField] private GameObject middleEnemyPrefab;
[SerializeField] private GameObject highEnemyPrefab;
public void CreateEnemy(int type, Vector3 position)
{
GameObject enemy = null;
switch (type)
{
case 1:
enemy = Instantiate(slowEnemyPrefab, position, Quaternion.identity);
enemy.tag = "SlowEnemy";
// enemy.AddComponent<EnemyBasics>();
enemy.GetComponent<EnemyBasics>().EnemySpeed = 1;
enemy.GetComponent<EnemyBasics>().EnemyAttackDistance = 1;
enemy.GetComponent<EnemyBasics>().EnemyChaseDistance = 8;
break;
case 2:
enemy = Instantiate(middleEnemyPrefab, position, Quaternion.identity);
enemy.tag = "MiddleEnemy";
//enemy.AddComponent<EnemyBasics>();
enemy.GetComponent<EnemyBasics>().EnemySpeed = 3;
enemy.GetComponent<EnemyBasics>().EnemyAttackDistance = 1;
enemy.GetComponent<EnemyBasics>().EnemyChaseDistance = 12;
// enemy.AddComponent<EnemyBasics>();
break;
case 3:
enemy = Instantiate(highEnemyPrefab, position, Quaternion.identity);
enemy.tag = "HighEnemy";
// enemy.AddComponent<EnemyBasics>();
enemy.GetComponent<EnemyBasics>().EnemySpeed = 5;
enemy.GetComponent<EnemyBasics>().EnemyAttackDistance = 1;
enemy.GetComponent<EnemyBasics>().EnemyChaseDistance = 20;
//enemy.AddComponent<EnemyBasics>();
break;
}
if (enemy != null)
{
enemy.layer = LayerMask.NameToLayer("Enemy");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemyControl : MonoBehaviour
{
private GameObject player;
[SerializeField] private Text gameOverText;
[SerializeField] private Image gameOverImage;
private EnemyFactory enemyFactory;
void Start()
{
player = GameObject.FindWithTag("Player");
enemyFactory = GetComponent<EnemyFactory>();
// 创建敌人实例
enemyFactory.CreateEnemy(1, new Vector3(0, 1, 0));
Debug.Log("初级敌人创建成功");
enemyFactory.CreateEnemy(2, new Vector3(10, 1, 0));
Debug.Log("中级敌人创建成功");
enemyFactory.CreateEnemy(3, new Vector3(20, 1, 0));
Debug.Log("高级敌人创建成功");
}
void Update()
{
if (player == null)
{
Debug.LogError("Player object is not assigned.");
return;
}
GameObject[] slowEnemys = GameObject.FindGameObjectsWithTag("SlowEnemy");
GameObject[] middleEnemys = GameObject.FindGameObjectsWithTag("MiddleEnemy");
GameObject[] highEnemys = GameObject.FindGameObjectsWithTag("HighEnemy");
HandleEnemies(slowEnemys);
HandleEnemies(middleEnemys);
HandleEnemies(highEnemys);
}
private void HandleEnemies(GameObject[] enemies)
{
foreach (GameObject enemy in enemies)
{
if (enemy != null )
{
float distance = Vector3.Distance(player.transform.position, enemy.transform.position);
Debug.Log(enemy.tag+" "+ distance);
enemy.GetComponent<EnemyBasics>().UpdateEnemy(player, distance, gameOverText, gameOverImage);
}
}
}
/* private void HandleEnemies(GameObject[] enemies)
{
foreach (GameObject enemy in enemies)
{
if (enemy != null)
{
EnemyBasics enemyBasics = enemy.GetComponent<EnemyBasics>();
if (enemyBasics != null)
{
float distance = Vector3.Distance(player.transform.position, enemy.transform.position);
Debug.Log($"{enemy.tag} - Distance: {distance}");
try
{
enemyBasics.UpdateEnemy(player, distance, gameOverText, gameOverImage);
}
catch (System.Exception ex)
{
Debug.LogError($"Error in UpdateEnemy for {enemy.name}: {ex.Message}");
}
}
else
{
Debug.LogError($"EnemyBasics component missing on {enemy.name}");
}
}
else
{
Debug.LogWarning("Encountered a null enemy in the array.");
}
}
}*/
}
挂载实现
预制体一览
2.我方实现
玩家控制代码
using UnityEngine;
public class PlayerController : MonoBehaviour
{
float CD = 2;
[SerializeField] private GameObject BombPre;
void Update()
{
//获取水平虚拟轴
float horizontal = Input.GetAxis("Horizontal");
//获取垂直虚拟轴
float vertical = Input.GetAxis("Vertical");
//创建一个向量,该向量为用户按下按键对应的向量
//这里让horizontal控制x轴移动,vertical控制z轴移动
Vector3 dir = new Vector3(vertical, 0, -horizontal);
//如果向量不为0,证明玩家一定按了方向键
if (dir != Vector3.zero)
{
//玩家转向dir向量的方向
transform.rotation = Quaternion.LookRotation(dir);
//Update是按帧刷新的,如果参数不乘以Time.deltaTime,代表的含义为每帧移动2m
//所以这里做一个转换,让2乘以帧时间间隔Time.deltaTi//即可转换单位,从每帧移动2m变为每秒移动2m
//转换单位还可以同步不同设备的速度,
//因为不同的设备可能有不同的帧率,所以如果不转换单位
//那么可能看到不同设备下的移动速度是不同的
transform.Translate(Vector3.forward * 7 * Time.deltaTime);
}
CD += Time.deltaTime;
//如果按下U键
if (Input.GetKeyDown(KeyCode.U))
{
Debug.Log("检测到玩家按下U键,放下炸弹");
//2s的CD
if (CD > 0.2f)
{
//重置CD
CD = 0;
//释放炸弹
Instantiate(BombPre, transform.position, transform.rotation);
}
}
}
}
挂载
摄像机代码(或者直接把摄像机变成玩家的子物体,就不用写代码了)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControllerr : MonoBehaviour
{
//保存向量
private Vector3 vecter3;
private Transform player;
void Start()
{
//两种方法找Player
// player = GameObject.Find("Player").transform;
player = GameObject.FindGameObjectWithTag("Player").transform;
vecter3 =player.transform.position-transform.position;
}
// Update is called once per frame
void Update()
{
transform.position = player.transform.position - vecter3;
}
}