problem with animation transition code

hello guys ,I really do need help so i was working on shooting script following tutorial from youtube … the script works fine but the animation won’t works the transition from idle animation to fire animation won’t happen

the script i amy using :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour {
          private Animator anim;
          public float range = 100f;
          public int bulletsPerMag = 30;
          public int bulletsleft;
          public int currentBullets;
         
          public Transform  shootPoint;
          public float fireRate = 0.1f;
          float fireTimer;
          void Start () {
                     anim = GetComponent<Animator>();
                     currentBullets = bulletsPerMag;         

           }
           void Update ()
          {
            if (Input.GetButton ("Fire1"))
            {
              Fire();
            }
           if (fireTimer < fireRate)
               fireTimer += Time.deltaTime;
          }
         
          void fixedUpdate()
           {
           AnimatorStateInfo info = anim.GetCurrentAnimatorStateInfo(0);
           if (info.IsName("Fire"))anim.SetBool("Fire",false);
           }
         private void Fire ()
         {
           if (fireTimer < fireRate) return;
           Debug.Log("fire-d!");

           RaycastHit hit;
           if (Physics.Raycast

(shootPoint.position,shootPoint.transform.forward, out hit , range))
           {
            Debug.Log(hit.transform.name+" found!");
           }
             
             anim.CrossFadeInFixedTime("Fire",0.5f);
             currentBullets--;
           fireTimer = 0.01f;
         }
}

A few issues.

  1. It’s FixedUpdate, not fixedUpdate, but you shouldn’t be using that either - only used FixedUpdate for physics-related stuff.
  2. So you need a bool that sets itself to false immediately after it’s used - use a Trigger type on the Animator instead.
  3. Instead of line 47, just use anim.SetTrigger(“Fire”). Don’t try to manipulate individual animations inside an Animator from your script - communicate with it via its properties instead. That’ll help avoid unpredictable behavior.

for ur second point the problem is that when I click on the right button the Fire bool won’t set to true , this is my problem I just want to know how to fix this part

And where in your code do you set the Fire bool to true? :slight_smile:

i do need to set it to true and then get it back to false ,if i set it true without making it off again it will keep shooting

I did try to sit it true then false in the fire function but it didn’t work

What I was saying was, there is no point in your code where you set it to true.

I did edit my code
it keeps shooting now without stop

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour {
          private Animator anim;
          public float range = 100f;
          public int bulletsPerMag = 30;
          public int bulletsleft;
          public int currentBullets;
         
          public Transform  shootPoint;
          public float fireRate = 0.1f;
          float fireTimer;
          void Start () {
                     anim = GetComponent<Animator>();
                     currentBullets = bulletsPerMag;         

           }
           void Update ()
          {
            if (Input.GetButton ("Fire1"))
            {
              Fire();
            }
           if (fireTimer < fireRate)
               fireTimer += Time.deltaTime;
          }
         
          void FixedUpdate()
           {
           AnimatorStateInfo info = anim.GetCurrentAnimatorStateInfo(0);
           if (info.IsName("Fire"))anim.SetBool("Fire",false);
           }
         private void Fire ()
         {
           if (fireTimer < fireRate) return;
           Debug.Log("fire-d!");

           RaycastHit hit;
           if (Physics.Raycast

(shootPoint.position,shootPoint.transform.forward, out hit , range))
           {
            Debug.Log(hit.transform.name+" found!");
           }
             
             
              anim.SetBool("Fire",true);
             currentBullets--;      
           fireTimer = 0.01f;
          
         }
}

Now it’s probably an AnimationClip or Animator transition issue. Does the Fire animation loop? And does the AnimatorController have a “if fire is false” transition out of the firing animation?

I only made if fire is true idle to shoot transition only

it’s prob in the code cos when I click the left button the Fire bool stay as true it didn’t change