キャラを破壊する


レイでキャラを破壊する。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerShot : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip effectSound;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
GameObject target = hit.collider.gameObject;
if (target.tag == "Chara")
{
Destroy(target.gameObject);
GameObject effect = Instantiate(effectPrefab, target.transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(effectSound, Camera.main.transform.position);
}
}
}
}
}





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


レイでキャラを破壊する。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerShot : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip effectSound;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
GameObject target = hit.collider.gameObject;
if (target.tag == "Chara")
{
Destroy(target.gameObject);
GameObject effect = Instantiate(effectPrefab, target.transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(effectSound, Camera.main.transform.position);
}
}
}
}
}





キャラを破壊する