ブログ

(Unity)マウスの左クリックで砲弾発射

(構成&設定)


(スクリプト)

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

public class ShotShell1 : MonoBehaviour
{
    public GameObject shellPrefab;
    public AudioClip sound;

    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            GameObject shell = Instantiate(shellPrefab, transform.position, Quaternion.identity);
            Rigidbody shellRb = shell.GetComponent<Rigidbody>();
            shellRb.AddForce(transform.forward * 500);
            Destroy(shell, 5.0f);
            AudioSource.PlayClipAtPoint(sound, transform.position);
        }
    }
}