-
Notifications
You must be signed in to change notification settings - Fork 0
/
play_sound.cs
executable file
·50 lines (42 loc) · 1.01 KB
/
play_sound.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using UnityEngine;
using System.Collections;
public class play_sound : MonoBehaviour {
private AudioSource sound;
// Use this for initialization
void Start () {
sound = this.gameObject.GetComponent<AudioSource>();
}
// Update is called once per frame
void Update () {
if(Input.touchCount>0) {
foreach (Touch touch in Input.touches) {
if (touch.phase == TouchPhase.Began) {
plsound(touch.position);
}
if (touch.phase == TouchPhase.Ended) {
stsound(touch.position);
}
}
}
}
void plsound(Vector3 x) {
Ray ray = Camera.main.ScreenPointToRay(x);
RaycastHit hit ;
if (Physics.Raycast (ray, out hit)) {
GameObject obj = hit.collider.gameObject;
if(obj==this.gameObject){
sound.Play();
}
}
}
void stsound(Vector3 x) {
Ray ray = Camera.main.ScreenPointToRay(x);
RaycastHit hit ;
if (Physics.Raycast (ray, out hit)) {
GameObject obj = hit.collider.gameObject;
if(obj==this.gameObject){
sound.Stop();
}
}
}
}