#It is driving me crazy But this code

1 messages · Page 1 of 1 (latest)

mint heath
#

Here

#
// Perform prepare logic
            if (isPreparing)
            {
                prepareTimer -= Time.deltaTime;

                if (prepareTimer <= 0)
                {
                    isPreparing = false;
                    enemyState = GroupAIState.MakingAttack;
                    chargingDirection = (target.position - transform.position).normalized;
                    chargingDurationTimer = chargingDuration;
                }
            }
            else if (enemyState == GroupAIState.MakingAttack)
            {
                Debug.Log("MakingAttack!");
                transform.position = Vector3.MoveTowards(transform.position, transform.position + chargingDirection, moveSpeed * chargeMultiplier * Time.deltaTime);
                chargingDurationTimer -= Time.deltaTime;
                if(chargingDurationTimer <= 0)
                {
                    enemyState = GroupAIState.RunningTowardsPlayer;
                }
            }
            else if (Vector2.Distance(transform.position, target.position) <= attackRange && !isPreparing)
            {
                isPreparing = true;
                enemyState = GroupAIState.PreparingAttack;
                prepareTimer = prepareTime;
            }
            else if(enemyState == GroupAIState.RunningTowardsPlayer)
            {
                transform.position = Vector3.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
            }
#

I never get MakingAttack debug log even though I supposed to get it. why might it be?

nimble summit
#

There is a lot of external variables we cant see that would affect this

mint heath
# nimble summit There is a lot of external variables we cant see that would affect this
public enum GroupAIState
    {
        FleeingFromPlayer,
        GroupingUp,
        RunningTowardsPlayer,
        PreparingAttack,
        MakingAttack
    }

    public float moveSpeed = 5f;
    public float attackRange = 4f;
    public float prepareTime = 1.4f;
    public float chargingDuration = 1.4f;
    public float chargeMultiplier = 2f;
    public float enemyGroupSearchRange = 20f;
    public float enemyGroupFoundRange = 2f;
    public float avoidRange = 5f;
    public float enemyCountsAsGroupRange = 5f;
    public LayerMask enemyLayerMask;
    public LayerMask playerLayerMask;
    private Transform target;
    private Transform playerTr;
    private bool isPreparing = false;
    private GroupAIState enemyState = GroupAIState.GroupingUp;
    private Vector3 chargingDirection;
    private float chargingDurationTimer;
    private float prepareTimer;
nimble summit
#

Ok but where do you change isPreparing

nimble summit
#

And prepareTimer

#

Oh

#

What is the other case

#

There is an else here

#

At yhe top

mint heath
nimble summit
#

This is called in update right?

#

And can you show me what object the script is attached to

mint heath
#

yes! and to clarify - my problem is that for some deleted weird reason I get isPreparing equals true in Running Towards Player state. The behaviour seems to be like in preparing state though. BUT IT IS SUPPOSED to just.. charge.. and attack...

#

Also charging duration timer never changes. WHY LOL

nimble summit
#

Alright well that should be plenty of info for someone to read- try using the rubber ducky method to find the issue maybe

#

Also

#

Try splitting code into smalll functions to make it easier to tell whats going on

#

That way i ususlly find bugs faster

mint heath
# nimble summit Also

I don't think this will help. I'm missing something fundamental here, like, logic error...

mint heath
#

Fixed~

nimble summit
#

What was the problem

nimble summit
#

Its easier to read code in pieces for me
And splitting into functions would make it easier to find logic problems

mint heath
nimble summit
#

Please elaborate