- Home /
The question is answered, right answer was accepted
State Machine If Else Distance question (c#)
Hi all. Having a bit of an issue with a state machine script. I need to determine the best way to differentiate between two similar distances for use in If/Else. Default state happens when > warningDistance (10f), but I also need another similar distance, safeDistance (11f) to be used for another state later on in the script, but I’m not sure how to make it unique. I tried adding a true/false bool with &&, but that didn’t work. safeDistance and warningDistance are very close to each other, but the script only seems to recognize warningDisance for First Action and never uses safeDistance to make it to the Fourth Action. Any ideas?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class StateDistanceTesting : MonoBehaviour {
public float safeDistance = 11f; //Not Close
public float warningDistance = 10f; //Kinda Close
public float dangerDistance = 2f; //Really Close
private bool checkDistance = true;
public GameObject bombObject; //Bomb
public GameObject playerObject; //Player
private Transform target;
private Animator anim;
public enum POActionType
{
FirstAction,
SecondAction,
ThirdAction,
FourthAction
}
private POActionType eCurState = POActionType.FirstAction;
void Start ()
{
anim = GetComponent<Animator>();
target = playerObject.transform;
}
void Update ()
{
switch (eCurState)
{
case POActionType.FirstAction:
HandleFirstAction();
break;
case POActionType.SecondAction:
HandleSecondAction();
break;
case POActionType.ThirdAction:
HandleThirdAction();
break;
case POActionType.ThirdAction:
HandleFourthAction();
break;
}
}
void HandleFirstAction()
{
if (checkDistance)
{
var distance = Vector3.Distance (bombObject.transform.position, transform.position);
if (distance > warningDistance)
{
anim.Play("1STACTION");
Debug.LogWarning("First Action");
}
else if (distance < warningDistance)
{
Debug.LogWarning ("Handle Second Action");
HandleSecondAction ();
}
}
}
void HandleSecondAction()
{
if (checkDistance)
{
var distance = Vector3.Distance (bombObject.transform.position, transform.position);
if ((distance <= warningDistance) && (distance > dangerDistance))
{
anim.Play ("2NDACTION");
eCurState = POActionType.SecondAction; //ANSWER THANKS TO ahorne: THIS LINE WAS MISSING
Debug.LogWarning ("Second Action");
}
else if (distance < dangerDistance)
{
Debug.LogWarning ("Handle Third Action");
HandleThirdAction ();
}
else if (distance > safeDistance) //this isn't getting called
{
Debug.LogWarning ("Handle Fourth Action");
HandleFourthAction ();
}
else if ((distance < safeDistance) && (distance > warningDistance))
{
Debug.LogWarning ("Handle Fourth Action");
HandleFourthAction ();
}
}
}
void HandleThirdAction()
{
anim.Play("3RDACTION");
eCurState = POActionType.ThirdAction; //ANSWER THANKS TO ahorne: THIS LINE WAS MISSING
Debug.LogWarning("Third Action");
}
void HandleFourthAction()
{
anim.Play("4THACTION");
eCurState = POActionType.FourthAction; //ANSWER THANKS TO ahorne: THIS LINE WAS MISSING
Debug.LogWarning("Fourth Action");
}
}
It's never getting called or it just doesn't get called if your distance is between 10 and 11? From what I can see the logic looks good except situations where your distance in between 10 and 11 because there is no condition to handle that.
I tried adding an else if ((distance < safeDistance) && (distance > warningDistance)) to cover that distance gap but it still ignores the Fourth Action and returns to the First Action.
Answer by ahorne · Jul 10, 2017 at 06:39 PM
Don't forget to change eCurState as your state changes. With your current code, every time through Update it will only ever use HandleFirstAction();