【サバイバルホラーゲームを作りたい。その11】

今回もこちらの動画を参考にやっていきます。
youtu.be

今回は発砲動作を実装します。

・マズルフラッシュを作ります。

・マズルフラッシュのアニメーションを作ります。

・下記スクリプトを追加します。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class InputScript : MonoBehaviour //EP6追加 InputObjectにアタッチ
{
    InputActions inputActions;

    bool interactionInput; // PlayerのInteractアクションのtrue/falseを代入
    bool fireInput; //Playerのfireアクションのtrue/falseを代入

    private void Awake()
    {
        if (inputActions == null)
        {
            inputActions = new InputActions();

            //Interactアクションの指定ボタンが押下された場合、InteractionInput = trueにする
            inputActions.Player.Interact.performed += i => interactionInput = true;
            //Interactアクションの指定ボタンを離した場合、InteractionInput = falseにする
            inputActions.Player.Interact.canceled += i => interactionInput = false;

            //fireアクションの指定ボタンが押下された場合、fireInput = trueにする
            inputActions.Player.Fire.performed += i => fireInput = true; //EP11追加
            //fireアクションの指定ボタンを離した場合、fireInput = falseにする
            inputActions.Player.Fire.canceled += i => fireInput = false; //EP11追加
        }

        //actionの追加
        inputActions.Enable();
    }

    public bool getInputActions()
    {
        return interactionInput;
    }

    //EP11追加
    public bool getFireInput()
    {
        return fireInput;
    }

    //EP11追加
    public void setFireInput(bool input)
    {
        fireInput = input;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FirePistol : MonoBehaviour //EP11追加 Gunにアタッチ
{
    public InputScript inputScript;
    Animator animator;
    public AudioSource pistolSound;

    private void Start()
    {
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        //左マウスが押された場合
        if (inputScript.getFireInput())
        {
            inputScript.setFireInput(false);
            animator.Play("PistolShot");
            animator.Play("MuzzleFlashAnim");
            pistolSound.Play();
        }
    }
}


プレイ動画です。
youtu.be


今回は以上です。