- Home /
Animation doesn't play
I have a shotgun shooting animation, but i can't get it to play. Here is my Animator component: Now here is my script:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class AimSys : MonoBehaviour
{
bool Click = false;
bool target = false;
public float fireRate = 0.5F;
private float nextFire = 0.0F;
public GameObject Flash;
public Animator anim;
void Update()
{
nextFire -= Time.deltaTime;
if (Input.GetButton("Fire1") && target && nextFire <= 0)
{
anim.SetTrigger("Shoot");
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
Flash.SetActive(true);
Debug.Log("Target Hit!");
nextFire = fireRate;
}
}
void OnCollisionEnter(Collision collision)
{
{
if (collision.gameObject.tag == "Enemy")
target = true;
}
}
void OnCollisionExit(Collision collision)
{
{
if (collision.gameObject.tag == "Enemy")
target = false;
Flash.SetActive(false);
}
}
}
What i want to do, is when the player click with the left mouse button (fire1), some effects are displayed such as sound effect and image effect, BUT ALSO i want my shooting animation to be played. The problem is that nothing happens! I didn't get any error, but still my animation just doesn't play. Could someone help me here?
Answer by Sergio7888 · Nov 08, 2016 at 06:24 PM
you need set the value of anim in Start or Awake.
void Start(){
anim=GetComponent<AudioSource>();
}
I also recommend make the audio a class field and set it in the start, you are setting it in update which is more expensive.
It is working, thanks! I can't believe i literally made a perfect animation in the exact time of my shooting audio just by hearing it. Anyway, thanks again!
Oh, by the way, i am beggining to use C# so i am kinda lost right now. I am going to update things to get more performance and make the game more smooth, but for now i am focusing on just making it work.
Would it not be an animator, rather than an audio source? Also, there seems to be someone down voting without justification.
Your answer
Follow this Question
Related Questions
Animation not be found 1 Answer
Running animation 1 Answer
On Hit play animation 0 Answers
Unity 5 Broken Animations? 3 Answers
Play a animation on an action? 1 Answer