How do I get an entity's IEnumerator to pause when the entity is seen?
I've got a villain type who teleports and inches towards the player unless they are seen, but I can't seem to figure out how to make them stop teleporting and moving when they are being rendered. I tried to find a way to use an inverted version of Renderer.isVisible, but couldn't make it work.
Here's the teleporter code as-is: void Start () { ballotBits = 0; rb = GetComponent(); StartCoroutine(SlenderWalk()); }
IEnumerator SlenderWalk()
{
yield return new WaitForSeconds(startWait);
while (true)
{
if (ballotBits == 0)
{
teleDist = Random.Range(startDist.x, startDist.y);
}
if (ballotBits == 1)
{
teleDist = Random.Range(firstDist.x, firstDist.y);
}
//.....
if (ballotBits == 7)
{
teleDist = Random.Range(seventhDist.x, seventhDist.y);
}
if (ballotBits == 8)
{
teleDist = Random.Range(eighthDist.x, eighthDist.y);
}
teleAngle = Random.Range(player.transform.eulerAngles.y+90f,player.transform.eulerAngles.y+270f);
PolarToCartesian(teleDist, teleAngle, out teleLoc);
rb.transform.position = new Vector3 (
player.transform.position.x + teleLoc.x,
Terrain.activeTerrain.SampleHeight(new Vector3(player.transform.position.x + teleLoc.x, 0, player.transform.position.z + teleLoc.y)) + 1.9f,
player.transform.position.z + teleLoc.y
);
yield return new WaitForSeconds(teleWait);
}
}
public static void PolarToCartesian(float radius, float polar, out Vector2 outCart)
{
polar = polar * Mathf.PI / 180;
outCart.x = radius * Mathf.Cos(polar);
outCart.y = radius * Mathf.Sin(polar);
}
I think that once I know how to pause this It'll be fairly simple to make it work in order to stop them from walking by just applying the same principles to the walking script.
Why are your Random.Range()
calls using x and y
vector values? The x and y
vector values could be easily equal or the y
value could be less
Why are you adding Vector3.z
values to Vector2.y
.
But to answer your question (and without anymore code to go by) you can do something like this (name variables and types to what you want)
public enum WalkingStatus {CancelWalking, Inactive, StartWalking, IsWalking};
// default to inactive
private WalkingStatus walkingStatus = WalkingStatus.Inactive;
void Start()
{
// set status to StartWalking
// this will start the coroutine in Update
// and change the status to IsWalking
SetWalkingStatus(WalkingStatus.StartWalking);
}
void SetWalkingStatus(WalkingStatus status)
{
walkingStatus = status;
}
void Update()
{
if (walkingStatus == WalkingStatus.CancelWalking)
{
StopCoroutine("SlenderWalk");
SetWalkingStatus(WalkingStatus.Inactive);
}
else if (walkingStatus == WalkingStatus.StartWalking)
{
SetWalkingStatus(WalkingStatus.IsWalking);
StartCoroutine(SlenderWalk);
}
}
To cancel/pause the coroutine just call
SetWalkingStatus(WalkingStatus.CancelWalking);
where needed (if you are doing this in Update, do it above the code provided).
Inside SlenderWalk()
change this
while (true)
to this
while (walkingStatus == WalkingStatus.IsWalking)