Create AIPrototype.cs

This commit is contained in:
mushgunAX 2022-11-01 12:09:50 +08:00
parent caf6006c9e
commit b0054d62c6
1 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using SHADE;
public class AIPrototype : Script
{
//This object's relevant components
private Transform transform;
[SerializeField]
[Tooltip("The list of waypoints that the object will move around on")]
private Vector3[] waypoints;
[SerializeField]
[Tooltip("How fast the object moves about waypoints")]
private float moveSpeed;
//To cycle depending on the length of waypoints
private int currentTargetWaypointIndex;
public AIPrototype(GameObject gameObj) : base(gameObj) { }
protected override void awake()
{
transform = GetComponent<Transform>();
if (transform == null)
{
Debug.LogError("Transform is NULL!");
}
currentTargetWaypointIndex = 0;
}
protected override void update()
{
//Head towards the next target
transform.GlobalPosition += (waypoints[currentTargetWaypointIndex] - transform.GlobalPosition) * moveSpeed * (float)Time.DeltaTime;
//Cycle to next waypoint if near enough
if ((waypoints[currentTargetWaypointIndex] - transform.GlobalPosition).GetSqrMagnitude() < 1.0f)
{
++currentTargetWaypointIndex;
if (currentTargetWaypointIndex == waypoints.Length)
{
currentTargetWaypointIndex = 0; //Recycle
}
}
}
}