欢迎光临散文网 会员登陆 & 注册

史上最全Unity3D教程

2022-07-05 08:50 作者:屑猫猫耳  | 我要投稿

c#代码

p140

EnemyMotor

using System.Collections;

using System.Collections.Generic;

using Unity.VisualScripting;

using UnityEngine;

using UnityEngine.SocialPlatforms;

/// <summary>

/// 敌人马达器,提供移动、旋转、寻路功能

/// </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>

  /// 注视旋转

  /// </summary>

  /// <param name="targetPoint">需要注视的目标点</param>

  public void targetPoint(Vector3 targetPoint)

  {

    this.transform.LookAt(targetPoint);

  }

  /// <summary>

  /// 寻路,沿路线移动

  /// </summary>

  /// <returns></returns>

  public bool Pathfinding()

  {

    targetPoint(points[i]);

    MovementForward();

    //你可以试试==0,会发生什么

    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>

  /// 找路线,没做不重复

  /// </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>

  /// 找路点

  /// </summary>

  /// <returns>Vector3[] 路点数组</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代替


史上最全Unity3D教程的评论 (共 条)

分享到微博请遵守国家法律