You would be better off if you explained what you are trying to achieve and your super generic post heading makes it sure no one else will ever find this thread… making this only helpful for you… so perhaps edit the heading?
Are you trying to simply set a random move direction based on random int value?
I didn’t bother to read too far, but the code seems a bit convoluted.
I would just have a list / array of directions (Vector2 values) and then I would pick a new direction delta from this list after enough time has passed, and each update I would call the Wander and it will move the character with delta. I wouldn’t put any direction selection logic in Wander method.
But if this works already for you, you can make it more understandable / shorter later. There is always another way to do the same thing.
“i will make sure to make the headlines a little more appropriate next time”
No need to wait for next time - next time won’t fix this thread’s heading; just press “Thread Tools” button on top right above your original post and change the heading to something descriptive.
Why do you only animate left and right?
Anyway, you should consider a Switch/Case logic to clean your code up a bit.
// Inside Wander()
switch(randomDir)
{
case 8:
transform.position += -transform.right * speed * Time.deltaTime;
transform.position += -transform.up * speed * Time.deltaTime;
break;
case 7:
transform.position += -transform.right * speed * Time.deltaTime;
transform.position += transform.up * speed * Time.deltaTime;
break;
case 6:
transform.position += transform.right * speed * Time.deltaTime;
transform.position += -transform.up * speed * Time.deltaTime;
break;
case 5:
transform.position += transform.right * speed*Time.deltaTime;
transform.position += transform.up * speed * Time.deltaTime;
break;
case 4:
transform.position += transform.up * speed*Time.deltaTime;
break;
case 3:
transform.position += transform.up * speed *Time.deltaTime;
break;
case 2:
anim.SetBool("IsRunningRight",false);
anim.SetBool("IsRunningLeft",true);
transform.position += -transform.right * speed*Time.deltaTime;
break;
case 1:
anim.SetBool("IsRunningRight",true);
anim.SetBool("IsRunningLeft",false);
transform.position += transform.right * speed*Time.deltaTime;
break;
default:
// Insert a fail-safe code to insure something happens
// Maybe pick a new random direction?
break;
}
Otherwise, consider else if statements rather than a bunch of if’s. Generally these are all the same, but I believe switch and else/if statements check more systematically. Which regular if statements will check every condition every single time regardless if a solution has returned true. Switch and else if stop checking once something returns true.
Is it just me or could you not move the whole if dead statements to the start of the update function so in case of death you do not have to do all the checks when it is dead.
Typing this on my phone so it is quite the pain to edit, anyways check enemystate if dead do nothing else run all the other code since they all assume the enemy state is not dead. That way at least you would avoid all the extra checks when enemy is set to dead.
Since it is in the update function, less code to run the better due to amount of calls, else I do not think it matters to much.
void Update()
{
If(currState != EnemyState.Die) {
switch (currState)
{
case (EnemyState.Wander):
Wander();
break;
case (EnemyState.Follow):
Follow();
break;
case (EnemyState.Die):
break;
}
if (IsPlayerInRange(range))
{
currState = EnemyState.Follow;
}
else
{
currState = EnemyState.Wander;
}
}
}