Hi,
I’m trying to make an object wait at its current location for a second or two before moving on the its next location. Applying a coroutine didn’t achieve the desired effect, maybe I’m just using it wrong. Please advise. Thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyProperties : MonoBehaviour
{
[Header("Movement Properties")]
public Transform[] waypoints;
public float speed;
public int curWaypoint;
public bool patrol = true;
public Vector3 target;
public Vector3 moveDirection;
public Vector3 velocity;
public float entryWait = 3f;
public float positionWait = 1f;
void Awake()
{
StartCoroutine(EntryWait());
}
void Update()
{
if (curWaypoint < waypoints.Length)
{
target = waypoints[curWaypoint].position;
moveDirection = target - transform.position;
velocity = GetComponent<Rigidbody>().velocity;
if (moveDirection.magnitude < 1)
{
curWaypoint = Random.Range(0, 14);
}
else
{
velocity = moveDirection.normalized * speed;
}
}
else
{
if (patrol)
{
curWaypoint = 0;
}
else
{
velocity = Vector3.zero;
}
}
GetComponent<Rigidbody>().velocity = velocity;
}
private IEnumerator EntryWait()
{
curWaypoint = 7;
yield return new WaitForSeconds(entryWait);
}
private IEnumerator PositionWait()
{
yield return new WaitForSeconds(positionWait);
}
}