Help Wanted (Playing Audio Clip when Clicked)

Random discussions.

Help Wanted (Playing Audio Clip when Clicked)

Postby SmartyPants » Fri Feb 17, 2017 2:21 pm

Hi , can anyone please help with the below code. I want a audio clip played everytime the user performs a mouse click , but it only ever seems to get played if the ray cast hits and object.

Any ideas please ?

using UnityEngine;
using System.Collections;
using TouchScript.Gestures;


public class Hit: MonoBehaviour {

public float Force = 10f;
public GameObject Prefab;
public AudioClip shootSound;

private TapGesture gesture;
private Rigidbody rb;
private Camera activeCamera;
private AudioSource source;
private float volLowRange = .5f;
private float volHighRange = 1.0f;

void Awake()
{
source = GetComponent<AudioSource>();

}

private void OnEnable()
{
rb = GetComponent<Rigidbody>();
activeCamera = GameObject.Find("Main Camera").GetComponent<Camera>();
gesture = GetComponent<TapGesture>();
gesture.Tapped += tappedHandler;
}

private void OnDisable()
{
gesture.Tapped -= tappedHandler;
}

private void tappedHandler(object sender, System.EventArgs e)
{
Debug.Log("tappedHandler");
float vol = Random.Range(volLowRange, volHighRange);
source.PlayOneShot(shootSound, vol);
var ray = activeCamera.ScreenPointToRay(gesture.ScreenPosition);

RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.tag == "blabla") // So will only destroy the if the tag matches
{
Instantiate(Prefab, hit.point, Quaternion.identity); // Play the explosion
Destroy(hit.transform.gameObject); // Destroy and remove

}

}
}
}
SmartyPants
 
Posts: 2
Joined: Tue Sep 27, 2016 11:33 am

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests