Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class BoundSound : MonoBehaviour{
  6.  
  7. //ぶつかったときのエフェクト
  8. [SerializeField] GameObject hitEffect; //追記
  9.  
  10. //壁の効果音
  11. [SerializeField] AudioClip wallSound; //追記
  12.  
  13. //音を再生するためのAudioSource
  14. private AudioSource hitSoundSource; //追記
  15.  
  16. //操作するためのRigidbody
  17. private Rigidbody rigidbody;
  18.  
  19.  
  20. void Start(){
  21. //音を再生するための下準備
  22. hitSoundSource = GetComponent<AudioSource>(); //追記
  23. rigidbody = GetComponent<Rigidbody>();
  24. }
  25.  
  26.  
  27. void Update() {
  28. //スペースキーが押されたとき自機を右上に飛ばす
  29. if (Input.GetKeyDown(KeyCode.Space)) {
  30. rigidbody.AddForce(0,400,500);
  31. }
  32. }
  33.  
  34. //ここから------------------------------------------------------------------------
  35. //コライダーとの当たり判定があったときに呼ばれる
  36. private void OnCollisionEnter(Collision collision){
  37. //ぶつかったときの処理
  38. hitSoundSource.PlayOneShot(wallSound);
  39.  
  40. //ぶつかった座標をピンポイントで取得して変数「point」に代入
  41. foreach (ContactPoint point in collision.contacts){
  42.  
  43. //pointの位置にエフェクトを生成
  44. Instantiate(hitEffect,point.point,Quaternion.identity);
  45. }
  46. }
  47. //ここまで追記--------------------------------------------------------------------
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement