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;
}
}
It’s FixedUpdate, not fixedUpdate, but you shouldn’t be using that either - only used FixedUpdate for physics-related stuff.
So you need a bool that sets itself to false immediately after it’s used - use a Trigger type on the Animator instead.
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
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?