0
\$\begingroup\$

I'm talking in build not in editor !

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

public class Shooting : MonoBehaviour
{
    [SerializeField]
    private Transform[] firePoints;
    [SerializeField]
    private Rigidbody projectilePrefab;
    [SerializeField]
    private float launchForce = 700f;
    [SerializeField]
    private Animator anim;
    [SerializeField]
    private bool automaticFire = false;
    [SerializeField]
    private bool slowDownEffect = false;

    private void Start()
    {
        anim.SetBool("Shooting", true);
    }

    public void Update()
    {
        if (isAnimationStatePlaying(anim, 0, "AIMING") == true)
        {
            if (Input.GetButtonDown("Fire1")/*automaticFire == false*/)
            {
                automaticFire = false;

                if (anim.GetBool("Shooting") == true)
                {
                    anim.Play("SHOOTING");
                    LaunchProjectile();
                }
            }
            else
            {
                if (/*automaticFire == true &&*/Input.GetButtonDown("Fire2"))
                {
                    automaticFire = true;
                }
            }
        }

        if(automaticFire == true)
        {
            anim.Play("SHOOTING");
            LaunchProjectile();
        }
    }

    private void LaunchProjectile()
    {
        foreach (var firePoint in firePoints)
        {
            Rigidbody projectileInstance = Instantiate(
                projectilePrefab,
                firePoint.position,
                firePoint.rotation);

            projectileInstance.AddForce(new Vector3(0, 0, 1) * launchForce);

            projectileInstance.gameObject.AddComponent<BulletDestruction>().Init();
        }
    }

    bool isAnimationStatePlaying(Animator anim, int animLayer, string stateName)
    {
        if (anim.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName))
            return true;
        else
            return false;
    }
}

I tried to do it in the Update using the automaticFire variable but it's not working fine.

When clicking first the left mouse button it's shooting singles but then when clicking the mouse right button it's shooting automatic but then when clicking the left mouse button again it's not back to single shots again it's staying on automatic.

The idea is right click mouse button automatic fire left button each time clicking the left mouse button the automatic should stop and then single shots with the left mouse button. And this way to toggle between the Fire1 and Fire2.

Fire1 = left mouse button click Fire2 = right mouse button.

\$\endgroup\$
0
\$\begingroup\$

So, if I understood correctly when you press the Mouse1 key once, the gun keeps on shooting continuously, instead if you click Mouse0 it stops and will only shoot again when you press the Mouse0 again. I'll base my code on this. Also, since I don't know how you set up the Animator, I'll change a few things that may not correspond to how you used to manage it, but it should make it more clear.

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

public class Shooting : MonoBehaviour
{
    [SerializeField]
    private Transform[] firePoints;
    [SerializeField]
    private Rigidbody projectilePrefab;
    [SerializeField]
    private float launchForce = 700f;
    [SerializeField]
    private Animator anim;
    [SerializeField]
    private bool automaticFire = false;
    [SerializeField]//ADDED NEW VARIABLES FOR FIRE RATE TO PREVENT SHOOTING EVERY SINGLE FRAME
    private float fireRate = 0.25f;
    private float lastFire = 0;
    [SerializeField]
    private bool slowDownEffect = false;

    private void Start()
    {
        anim.SetBool("Aiming", true);//CHANGED NAME, IF WHAT IT DOES IS PLAYING THE AIMING STATE
    }

    private void Update()
    {
        if (isAnimationStatePlaying("AIMING", 0))
        {
            if(Input.GetMouseDown(0))//LEFT CLICKING
            {
                if(automaticFire)
                {
                    automaticFire = false;
                }
                else
                {
                    anim.Play("SHOOTING");
                    LaunchProjectile();
                }
            }

            if(Input.GetMouseDown(1) && !automaticFire)//RIGHT CLICKING
                automaticFire = true;

            if(automaticFire)
            {
                if(Time.time > lastFire + fireRate)
                {
                    anim.Play("SHOOTING");
                    LaunchProjectile();
                    lastFire = Time.time;
                }
            }
        }
    }

    private void LaunchProjectile()
    {
        foreach (var firePoint in firePoints)
        {
            Rigidbody projectileInstance = Instantiate(
                projectilePrefab,
                firePoint.position,
                firePoint.rotation);

            projectileInstance.AddForce(new Vector3(0, 0, 1) * launchForce);

            projectileInstance.gameObject.AddComponent<BulletDestruction>().Init();
        }
    }

    bool isAnimationStatePlaying(string stateName, int animLayer)
    {
        if (anim.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName))
            return true;
        else
            return false;
    }

}
  • As you may have noticed, I have changed the anim.SetBool("Shooting", true); in the Start void since, from what I understand, what it does is simply making the animator play the "AIMING" state.
  • Added a fireRate system so that the when you're automatic firing you won't instantiate a new projectile every single frame.
  • I modified the isAnimationStatePlaying function since passing the animator is pointless as it is a global variable.
  • One more suggestion is to attach the BulletDestruction component to the bulletPrefab so that you won't have to do it by code every time you shoot a new bullet.

If this doesn't answer your question then probably you need to upload an image of how your Animator is set up and better explain how this code is supposed to relate with other scripts in your scene that deal with the animator as well.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.