@作者 : SYFStrive
@博客首页 :
子弹尾线出现残影的问题
using UnityEngine; public class PlayerProjectile : Projectile { private TrailRenderer trilRenderer; protected virtual void Awake() { //获取对应的组件 trilRenderer=GetComponentInChildren(); if (moveDirection != Vector3.right) { transform.GetChild(0).rotation = Quaternion.FromToRotation(Vector3.right, moveDirection); } } //被禁用调用 private void OnDisable() { //清楚拖尾 trilRenderer.Clear(); }}
效果如
敌人控制器制作
实现:完成敌人生成初始点、随机生成,发射子弹
控制敌人的飞行范围
Viewport 视图添加代码如
using UnityEngine;public class Viewport : Singleton{ //获取X轴的中间位置 private float centerPosX; private void Start() { //获取X轴的中间位置 centerPosX = camera.ViewportToWorldPoint(new Vector3(0.5f, 0f, 0f)).x; } ///
/// 敌人随机的初始化位置 /// public Vector3 RandomEnemyInitializePos(float paddingX,float paddingY) { //初始化位置 Vector3 intaialPosition = Vector3.zero; //paddingX、paddingY 限制飞机一半身体超出视口外偏移量 intaialPosition.x = maxX + paddingX; intaialPosition.y = Random.Range(minY + paddingY, maxY - paddingY); //返回位置 return intaialPosition; } ////// 限制敌人的移动范围 /// /// public Vector3 EnemyRandomMoveScope(float paddingX, float paddingY) { //初始化位置 Vector3 MoveScopePosition = Vector3.zero; //paddingX、paddingY 限制飞机一半身体超出视口外偏移量 MoveScopePosition.x = Random.Range(centerPosX,maxX-paddingX); MoveScopePosition.y = Random.Range(minY + paddingY, maxY - paddingY); //返回位置 return MoveScopePosition; } ////// 限制敌人的移动范围 /// /// public Vector3 EnemyRandomMove(float paddingX, float paddingY) { //初始化位置 Vector3 MoveScopePosition = Vector3.zero; //paddingX、paddingY 限制飞机一半身体超出视口外偏移量 MoveScopePosition.x = Random.Range(minX + paddingX, maxX - paddingX); MoveScopePosition.y = Random.Range(minY + paddingY, maxY - paddingY); //返回位置 return MoveScopePosition; }}控制敌人的移动发射子弹
EnemyController 敌人管理器添加代码如
using System.Collections;using System.Collections.Generic;using UnityEngine;public class EnemyController : MonoBehaviour{ [Header("---Move---")] [SerializeField] float paddingX; [SerializeField] float paddingY; [SerializeField] float moveSpeed = 2f; [SerializeField] float rositionAngle = 25f; [Header("---Fire---")] [SerializeField] float maxFireTime=2; [SerializeField] float minFireTime=3; [SerializeField] GameObject[] enemyProjectileS; [SerializeField] Transform enemyFirePosition; private void OnEnable() { //敌人随机移动携程 StartCoroutine(nameof(EnemyRandomMovePosIE)); //开启敌人射击携程 StartCoroutine(nameof(EnemyRandomFireIE)); } private void OnDisable() { //关闭所有携程 StopAllCoroutines(); } IEnumerator EnemyRandomMovePosIE() { //敌人初始化的位置 transform.position = Viewport.Instance.RandomEnemyInitializePos(paddingX, paddingY); //初始化移动目标的位置 Vector3 targetPosition = Viewport.Instance.EnemyRandomMoveScope(paddingX, paddingY); while (gameObject.activeSelf) { //PseudoCode伪代码(思路清晰) //敌人还没到达目标位置 那么继续移动 //Mathf.Epsilon 接近与零的数 if (Vector3.Distance(transform.position, targetPosition) > Mathf.Epsilon) { //移动 transform.position = Vector3.MoveTowards(transform.position, targetPosition, moveSpeed * Time.deltaTime); //旋转 transform.rotation = Quaternion.AngleAxis((targetPosition - transform.position).normalized.y * rositionAngle, Vector3.right); } else //如果敌人到达了目标位置那么继续寻找一个新的目标移动 targetPosition = Viewport.Instance.EnemyRandomMoveScope(paddingX, paddingY); yield return null; } } IEnumerator EnemyRandomFireIE() { while (gameObject.activeSelf) { yield return new WaitForSeconds(Random.Range(minFireTime, maxFireTime)); foreach (var enemyProjectile in enemyProjectileS) { //使用对象池 PoolManager.Release(enemyProjectile, enemyFirePosition.position); } } }}
敌人子弹脚本
using UnityEngine;public class EnemyProjectile : Projectile{ private void Awake() { if(moveDirection != Vector3.left) { transform.rotation = Quaternion.FromToRotation(Vector3.left, moveDirection); } }}
添加敌人子弹的对象池
PoolManager添加代码如
[SerializeField] Pool[] enemyPoolProjectile; private void OnEnable() { //初始化对象池 InitializeObj(enemyPoolProjectile); } #region 测试代码#if UNITY_EDITOR //停止游戏时执行 private void OnDestroy() { CheckPoolSize(enemyPoolProjectile); }#endif #endregion
效果
最后
本文到这里就结束了,大佬们的支持是我持续更新的最大动力,希望这篇文章能帮到大家下篇文章再见ヾ( ̄▽ ̄)ByeBye