Hihi, i’m doing on a AI that moves to a waypoint and chooses if it wants to stay there for a few seconds. after that it will continue moving using the waypoints. i’m kinda stuck on the delay and checking for waypoints. can someone help me?
below is my codes. ps. sorry for my bad naming convention
`using UnityEngine;
using System.Collections;
public class AI_Way_Point_Movement : MonoBehaviour {
private int currentWayPoint;
private float timeWait = 0.0f;
int ranGenerator = 0;
float speed = 10.0f;
bool loop = true;
Vector3 target;
Vector3 moveDirection;
Vector3 velocity;
public Transform[] wayPoint;
// Use this for initialization
void Start ()
{
}
void Awake()
{
}
// Update is called once per frame
void Update ()
{
upDateWayPoint();
}
void upDateWayPoint()
{
if (target.magnitude == 1 )
{
ranGenerator = Random.Range(0, 2);
if (ranGenerator == 0)
{
NextWayPoint();
}
else
if (ranGenerator == 1)
{
StayWayPoint();
}
}
}
void NextWayPoint()
{
if (currentWayPoint < wayPoint.Length)
{
target = wayPoint[currentWayPoint].position;
moveDirection = target - transform.position;
velocity = rigidbody.velocity;
if (moveDirection.magnitude < 1)
{
currentWayPoint++;
}
else
{
velocity = moveDirection.normalized * speed;
}
}
else
{
if (loop)
{
currentWayPoint = 0;
}
else
{
velocity = Vector3.zero;
}
}
rigidbody.velocity = velocity;
transform.LookAt(target);
}
//
void StayWayPoint()
{
float randomGen = Random.Range(0.5f, 1.0f);
timeWait = randomGen;
if (timeWait != 0)
{
timeWait -= Time.smoothDeltaTime;
speed = 0.0f;
}
else
{
speed = 10.0f;
}
}
}
`