I’m new to C# and programming in general, so I’d appreciate any explanations you can give, so I can learn something.
I am using a state machine I’ve made that switches between two very simple states for an enemy, CHASING and EVADING, it switches between them based on a really simple bit of code that converts a vector3 to a float. The scripts being enabled in each state work fine independently but I’m using the following code to decide how to switch between states and as I’m sure you’ve probably already realised… it flips between them like it’s having a seizure.
I know WHY it’s doing it, because it’s flip flopping between 300 and 301 and everything in between, my question is, there has to be a better way of doing it that isn’t what I’m doing, I’ve experimented with timers, bools, everything I can think of, but it all comes down to the issue of the distance.
Like I said I’m new, all I really want is to be pointed in the right direction
I’m never done anything like this in a game, but I have thought about it a number of times. One of the things I considered was your flip-flop scenario
I think in my hypothetical ideas, it was about being in combat range or getting closer.
Yours is a bit confusing. First, you want to go towards the player if you’re far away. But then if you get close, you want to run away. lol. I could help your AI by saying it can save time by just staying away if that its logic
Anyhow, besides that… I will try to give an answer:
I’d try to move towards, say 300 (long range, but… for the example)
then, when i’m very close to 300, I’m set.
Now, if the player >= 350, I start chasing, again.
Note: Obviously it doesn’t have to be “350” - that is just an example of some leeway to give…
I’m still very confused about approaching then running away heh. Sorry…
There are a few different ways besides distance, like line of sight, but they are more complicated. Yeah, you don’t have a buffer zone, like Methos said, so that’s why you get that behavior. The other is that you have two states, chase and run away, which is kind of unnatural. Maybe a third state where they patrol, which is to move between waypoints. If games do have 3 states, it’s usually patrol, chase, attack, then return to patrol.
The enemy behaviour is just the first one I’ve started working on, I wanted it to charge towards firing, and then dart away when it gets close, then do it again.
Having a buffer state to do something else didn’t really occur to me, thanks, Fire7side, I’ll give that a go, sort of had horse blinkers on trying to find a solution - cheers.
First off you should post all relevant code here within [ code ] tags. Make certain to edit your original post too.
Second it is always a good idea to post all relevant code too.
But on to your problem. You said in your original post that you converted a Vector3 to a float. I am assuming you have made some assumptions about Vector3’s that are not true, because one cannot just change a Vector3 to a float, as a V3 contains three different component floats. If you’re looking for the distance between your target and your fighter you should do this:
Vector3 dir = target.transform.position - fighter.transform.position;
float dist = dir.magnitude;
Also I think the reason your fighter has been flip flopping rapidly is because you are only comparing one of those 3 vector components. Those are bound to change with movement.
Lastly use
if (dist.CompareTo(yourBoundaryFloat) == 0 ) //Test for equality (can also do float.Equals(value)
{
}
else if (dist.CompareTo(yourBoundaryFloat) == -1 ) //Test for you dist value being less than your boundary
{
}
else if ((dist.CompareTo(yourBoundaryFloat) == +1) // Test for greater than
to compare your floats, as the basic comparison operators do not always work on floats.