My aim is to basically make an idle shooter game (rail shooter) where an enemy appears on the screen and when the player simply clicks the enemy, a dying animation is played once and then the enemy leaves the screen.
using System.Collections;
using UnityEngine;
using UnityEngine.Animations;
public class ClickAndDestroy : MonoBehaviour
{
public Animation DyingAnimation;
public string Dying;
Animation anim;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
BoxCollider bc = hit.collider as BoxCollider;
if (hit.transform.tag == "Enemy")
{
Debug.Log("Dead!");
anim["Dying"].wrapMode = WrapMode.Once;
anim.Play("Dying");
Animation.Play(DyingAnimation);
Destroy(bc.gameObject);
}
}
}
}
}
This is my code right now which is attached to the main camera. However, when I click the enemy, it disappears but the dying animation is not played. Please help