Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class BoundSound : MonoBehaviour{
- //ぶつかったときのエフェクト
- [SerializeField] GameObject hitEffect; //追記
- //壁の効果音
- [SerializeField] AudioClip wallSound; //追記
- //音を再生するためのAudioSource
- private AudioSource hitSoundSource; //追記
- //操作するためのRigidbody
- private Rigidbody rigidbody;
- void Start(){
- //音を再生するための下準備
- hitSoundSource = GetComponent<AudioSource>(); //追記
- rigidbody = GetComponent<Rigidbody>();
- }
- void Update() {
- //スペースキーが押されたとき自機を右上に飛ばす
- if (Input.GetKeyDown(KeyCode.Space)) {
- rigidbody.AddForce(0,400,500);
- }
- }
- //ここから------------------------------------------------------------------------
- //コライダーとの当たり判定があったときに呼ばれる
- private void OnCollisionEnter(Collision collision){
- //ぶつかったときの処理
- hitSoundSource.PlayOneShot(wallSound);
- //ぶつかった座標をピンポイントで取得して変数「point」に代入
- foreach (ContactPoint point in collision.contacts){
- //pointの位置にエフェクトを生成
- Instantiate(hitEffect,point.point,Quaternion.identity);
- }
- }
- //ここまで追記--------------------------------------------------------------------
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement