I have a script that shoot bullets but how can I make that it will shoot with the animation ?

The script is working fine. I can shoot using the mouse left button click or automatic.

The problem will be now to make it shoot automatic according to the object animation.
When the drone is playing the shooting animation the childs Pod_Weapon_L_Tip1 and Pod_Weapon_R_Tip1 are moving forward and backward like shooting.

My problem is how to fit my shooting script so it will shoot according to the Pod_Weapon_L_Tip1 and R moving. In my logic it should shoot when they are moving forward in the animation.

I don’t ant to make the shooting as part of the animation but to fit it to shoot when they are moving forward and according to the Pod_Weapon_L_Tip1 and R speed.

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using UnityEditor;
using UnityEngine;

public class ShootBullets : MonoBehaviour
{
    public float bulletSpeed;
    public bool automaticFire = false;
    public float fireRate;
    public Rigidbody bullet;

    private float gunheat;
    private List<GameObject> startPositions;
    private bool shoot = false;
    private GameObject bulletsParent;
    private GameObject startpos;

    // Start is called before the first frame update
    void Start()
    {
        startPositions = GameObject.FindGameObjectsWithTag("Bullet").ToList();
        bulletsParent = GameObject.Find("Bullets");
        startpos = GameObject.Find("Pod_Weapon_L_Tip1");
    }

    void Fire()
    {
        Rigidbody bulletClone = (Rigidbody)Instantiate(bullet, startpos.transform.position, startpos.transform.rotation);
        bulletClone.velocity = transform.forward * bulletSpeed;

        Destroy(bulletClone.gameObject, 0.5f);
    }

    // Update is called once per frame
    void Update()
    {
        if (automaticFire == false)
        {
            if (Input.GetMouseButtonDown(0))
            {
                Fire();
            }
        }
        else
        {
            if (shoot == true)
            {
                    Fire();

                    shoot = false;
            }
        }

        if (gunheat > 0) gunheat -= Time.deltaTime;

        if (gunheat <= 0)
        {
            shoot = true;
            gunheat = fireRate;
        }
    }
}

You should use animation events. On your imported model there’s an animation tab. Go to events and you can select where in the animation you want a function to run. Just make sure to have the script that contains the function on your character. So in this case you’d write “Fire” as your event. Hope that helps.

1 Like