I'm in big BIG trouble

void OnTriggerEnter2D( Collider2D other){
        if (other.tag == "enemy") {
            GameObject clone = Instantiate (bullet, transform.position, transform.rotation);
    timeBtwShots = startTimeBtwShots;
            } else {

                timeBtwShots -= Time.deltaTime;

hi guys, I’m having trouble with my attack animation to run at the same time as my bullet gets instantiated. I intend that each time the enemy’s attack animation plays, a bullet also gets instantiated.

But, its either a barrage of bullets get instantiated as the animation plays or its just the bullets or the animation.

What I did is, I have a CHILD attached to the enemy, its function is to detect the target’s approach by having the “isTrigger” checked in the boxcollider.

Once the target entered the boxCollider, the enemy will shoot bullets synchronized with the animation.

You gotta give us more to run on, gow are we suppose to know whats wrong if we can’t see any of the functions at play here?

Its like going to the doctor and saying “I know a sick person and i need something to help him”

1 Like

here’s the script I don’t know but for me it seems logical enough…
Animator anim;
public GameObject bullet;
Rigidbody2D rb;
public float speed;

private float timeBtwShots;
public float startTimeBtwShots;

void Awake () {
rb = GetComponent ();
enemy = GameObject.FindGameObjectWithTag (“enemy”).transform;
timeBtwShots = startTimeBtwShots;

}
void Start () {

anim = GetComponent ();

}

        void OnTriggerEnter2D(Collider2D other) {
        if (other.tag == "enemy") {
            anim.SetBool ("isAttacking", true);
                  
                    Instantiate (bullet, transform.position, transform.rotation);
                    timeBtwShots = startTimeBtwShots;
                }else{
                    anim.SetBool ("isAttacking", false);
                    timeBtwShots -= Time.deltaTime;
                }

                    }

my BIGGEST TROUBLE actually is how to synchronize the release of the bullet with my attack animation…

Did you try adding an event to your attack animation that makes the bullet be fired when the animation starts?

Also, note that your OnTriggerEnter will not run each frame.
Which means that this will not decrement correctly → timeBtwShots -= Time.deltaTime;

thanks guys I think my attack animation runs at the same time now BUT…

        anim.SetBool ("isAttacking", true);
                    timeBtwShots = startTimeBtwShots;
                } else {
                    anim.SetBool ("isWalking", true);
                    timeBtwShots -= Time.deltaTime;

I don’t know how to animate back to walking…

it either gets stuck to attack once activated or it starts from walking and never animates attack…im using bool and I think i set it up right

Idle → attack = isAttacking true
attack → Idle = isAttacking false
attack → walk = isAttacking false
walk → attack = isAttacking true
Idle → walk= isWalking true
walk → Idle = isWalking false

APSchmidt hi…no I haven’t but I think its synchronized now my only problem is my enemy couldnt animate back to walking

Use a .SetTrigger() and trigger instead of bool as a property inside your controller.
Add a backward transition from your attacking state to the walking state (if it doesn’t exist), and you should be good.

thanks guys i found the solution

hi guys just an update, I tried the scene animation as suggested by vergil, everything went fine with synchronization until its time to drain my hp with each attack, I did check just 1 frame of the attack animation collider to make that the point the hp gets drained but the hp still drains so fast

this is the 2 scripts, I have a player who will be attacked and an enemy…the enemy has 2 child, 1 fot the weapon the other to detect the player trigger…the player has setTrigger, the 2 child has setTrigger each with the attack script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Attack : MonoBehaviour {

    void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject.name.Equals("Girl"))
        {
            Zombie.isAttacking = true;
        }
    }

    void OnTriggerExit2D(Collider2D col)
    {
        if (col.gameObject.name.Equals("Girl"))
        {
            Zombie.isAttacking = false;
        }
    }
}

the enemy reverts back to walking which solved my previous issue, but the health system gets drained so fast I even tried to use prefab and fade script on it

If a lot of firing is taking place this could still slow down if you keep instantiating new bullets as they are fired. You could instantiate a pool of bullets earlier on, that are then used in the code.