敵を破壊する


レイを使って敵を破壊する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerShot : MonoBehaviour
{
    public GameObject effectPrefab;
    public AudioClip sound;
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                GameObject target = hit.collider.gameObject;
                if (target.CompareTag("Enemy"))
                {
                    Destroy(target.gameObject);
                    GameObject effect = Instantiate(effectPrefab, hit.point, Quaternion.identity);
                    Destroy(effect, 0.5f);
                    AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
                }
            }
        }
    }
}


【2022版】AR_Project(全9回)
| 1 | キャラクターをAR鑑賞する | 
| 2 | ★チャレンジ課題 | 
| 3 | ARシューティングゲームの開発 | 
| 4 | ★チャレンジ課題 | 
| 5 | 敵の製造装置を作る | 
| 6 | 敵を破壊する | 
| 7 | オリジナルのカーソルを作成する | 
| 8 | カウンターを作成する | 
| 9 | ★チャレンジ課題 | 


レイを使って敵を破壊する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerShot : MonoBehaviour
{
    public GameObject effectPrefab;
    public AudioClip sound;
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                GameObject target = hit.collider.gameObject;
                if (target.CompareTag("Enemy"))
                {
                    Destroy(target.gameObject);
                    GameObject effect = Instantiate(effectPrefab, hit.point, Quaternion.identity);
                    Destroy(effect, 0.5f);
                    AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
                }
            }
        }
    }
}


敵を破壊する