史上最全Unity3D教程

c#代碼
p140
EnemyMotor
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SocialPlatforms;
/// <summary>
/// 敵人馬達(dá)器,提供移動(dòng)、旋轉(zhuǎn)、尋路功能
/// </summary>
public class EnemyMotor : MonoBehaviour
{
??private Vector3 startingPoint;
??private Vector3 destination;
??private WayLine wayLine;
??private Vector3[] points;
??private EnemySpawn enemySpawn;
??private bool isMove;
??public float nextTime;
??private int i = 0;
??/// <summary>
??/// 前行
??/// </summary>
??public void MovementForward()
??{
????this.transform.Translate(0, 0, (float)0.02);
??}
??/// <summary>
??/// 注視旋轉(zhuǎn)
??/// </summary>
??/// <param name="targetPoint">需要注視的目標(biāo)點(diǎn)</param>
??public void targetPoint(Vector3 targetPoint)
??{
????this.transform.LookAt(targetPoint);
??}
??/// <summary>
??/// 尋路,沿路線移動(dòng)
??/// </summary>
??/// <returns></returns>
??public bool Pathfinding()
??{
????targetPoint(points[i]);
????MovementForward();
????//你可以試試==0,會(huì)發(fā)生什么
????if (Vector3.Distance(this.transform.position, points[i]) <=0.02)
??????i++;
????if (i >= 4)
??????return false;
????else
??????return true;
??}
??// Start is called before the first frame update
??void Start()
??{
????isMove=true;
????startingPoint = this.transform.position;
????nextTime = 1;
????enemySpawn =Object.FindObjectOfType<EnemySpawn>();
????wayLine = enemySpawn.FingWayLine();
????points = wayLine.GetWayPoints();
??}
??// Update is called once per frame
??void Update()
??{
????if (isMove)
????{
??????isMove = Pathfinding();
????}
??}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
using UnityEngine;
/// <summary>
/// 敵人生成器
/// </summary>
public class EnemySpawn : MonoBehaviour
{
??private WayLine[] wayLines;
??private System.Random random;
??private int firstNum;
??private int finallyNum;
??public int nextTime;
??// Start is called before the first frame update
??//private Vector3 point;
??//private Vector3[] points;
??//private WayLine wayLine;
??void Start()
??{
????//wayLine=FingWayLine();
????//points = wayLine.Points;
????//for (int i = 0; i < points.Length; i++)
????//{
????//point = points[i];
????//Debug.Log(point);
????//}
??}
??/// <summary>
??/// 找路線,沒(méi)做不重復(fù)
??/// </summary>
??public WayLine FingWayLine()
??{
????wayLines = this.transform.GetComponentsInChildren<WayLine>();
????random = new System.Random();
????firstNum = 0;
????finallyNum = wayLines.Length;
????int usedNum = random.Next(firstNum, finallyNum);
????wayLines[usedNum].IsUseable = false;
????Debug.Log(wayLines[usedNum]);
????//if (usedNum == firstNum)
??????//firstNum += 1;
????//else if (usedNum == finallyNum)
??????//finallyNum -= 1;
????return wayLines[usedNum];
??}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using UnityEngine;
/// <summary>
/// 路線類
/// </summary>
public class WayLine : MonoBehaviour
{
??private Vector3[] Points;
??public bool IsUseable;
??private Transform[] PointsTF;
??private Vector3[] newPoints;
??// Start is called before the first frame update
??void Start()
??{
????PointsTF = this.transform.GetComponentsInChildren<Transform>();
????Points = new Vector3[PointsTF.Length];
????newPoints = new Vector3[PointsTF.Length - 1];
????IsUseable=true;
??}
??/// <summary>
??/// 找路點(diǎn)
??/// </summary>
??/// <returns>Vector3[] 路點(diǎn)數(shù)組</returns>
??public Vector3[] GetWayPoints()
??{
????for (int i = 0; i < PointsTF.Length; i++)
????{
??????Points[i] = PointsTF[i].position;??
????}
????for (int i = 0; i < newPoints.Length; i++)
????{
??????newPoints[i] = Points[i + 1];
????}
????return newPoints;
??}

敵人可以用Cube代替