Skip to content
Snippets Groups Projects
ArrowSpawner.cs 1.23 KiB
Newer Older
using UnityEngine;

// Used by the Animator for the Archer animation in the Shoot state. Fires an
// Arrow once the accoring state is entered/animation is played.
public class ArrowSpawner : StateMachineBehaviour
{
    [SerializeField] GameObject arrowPrefab;
    GameObject arrow;

    public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        if (stateInfo.IsName("shoot"))
        {
Moritz Meyerhof's avatar
Moritz Meyerhof committed
            int directionArrow = 1;
            if (animator.gameObject.transform.localScale.x < 0f)
            {
                directionArrow = -1;
            }
            Vector2 spawnPos = animator.gameObject.transform.position;
            spawnPos += new Vector2(1 * directionArrow, 0);

            arrow = Instantiate(arrowPrefab, spawnPos, Quaternion.identity);
            ChangeArrowDirection(arrow, directionArrow);
            ArrowController arrowController = arrow.GetComponentInChildren<ArrowController>();
            arrowController.direction = directionArrow;
            arrowController.archer = animator.gameObject.GetComponent<Archer>();
Moritz Meyerhof's avatar
Moritz Meyerhof committed

    void ChangeArrowDirection(GameObject arrow, int directionArrow)
    {
        arrow.transform.Rotate(0, 0, 90 * directionArrow);