- Home /
Question by
bigsaqib1983 · Jan 27, 2019 at 10:19 PM ·
c#animationprogramming
My animation doesn't play!
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
void Shoot()
{
RaycastHit hit;
Physics.Raycast(Raycastorigin.transform.position, Raycastorigin.transform.forward);
muzzleFlash.Play();
GetComponent<Animator>().Play("GUNPLAY!");
animation.enabled = true;
animation.Play("GUNPLAY!");
}
}
untitled.jpg
(194.4 kB)
Comment
Best Answer
Answer by UnityCoach · Jan 27, 2019 at 03:51 PM
Ok, let me put this back in proper form to make it readable and add comments.
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
} // your code is missing this closing bracket
void Shoot()
{
RaycastHit hit; // you're not using this RaycastHit object
Physics.Raycast(Raycastorigin.transform.position, Raycastorigin.transform.forward); // you're not doing anything with the result of this method
muzzleFlash.Play();
GetComponent().Play("GUNPLAY!"); // GetComponent needs a little hint to figure which component it's meant to fetch
animation.enabled = true; // we don't know what animation is, is it an Animation/Animator component ?
animation.Play("GUNPLAY!");
} // your code is missing this closing bracket
Please watch a tutorial on Animator State Machine, there are some good and free ones on Unity's Learn page.