Reputation: 43
the sound is not playing for the second touch it only plays for the first touch the sound is only 0.02 duration and its in mp3 it plays only for first touch but i have to make it for each click and it should feel like accelarator
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
// The force which is added when the player jumps
// This can be changed in the Inspector window
public Vector2 jumpForce = new Vector2(0, 300);
public AudioClip imp;
public AudioSource clk;
// Update is called once per frame
void Update ()
{
// Jump
if (Input.GetMouseButtonDown(0))
{
GetComponent<Rigidbody2D>().velocity = Vector2.zero;
GetComponent<Rigidbody2D>().AddForce(jumpForce);
clk.PlayOneShot (imp, 0.7f);
}
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (screenPosition.y > Screen.height || screenPosition.y < 0)
{
Die();
}
}
// Die by collision
void OnCollisionEnter2D(Collision2D other)
{
Die();
}
void Die()
{
Application.LoadLevel(0);
}
}
Upvotes: 0
Views: 283
Reputation: 125455
The sound is not playing for the second touch because Input.GetMouseButtonDown(0) will only detect one touch. You loop through the Touches
then play sound if one is pressed. Break the loop after the first touch is detected since you only one to play one sound when there is a touch on the screen.
void Update()
{
int touches = Input.touchCount;
Debug.Log(touches);
for (int i = 0; i < touches; i++)
{
if (touches > 0 && Input.GetTouch(i).phase == TouchPhase.Began)
{
clk.PlayOneShot(imp, 0.7f);
break;
}
}
if (Input.GetMouseButtonDown(0))
{
GetComponent<Rigidbody2D>().velocity = Vector2.zero;
GetComponent<Rigidbody2D>().AddForce(jumpForce);
}
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (screenPosition.y > Screen.height || screenPosition.y < 0)
{
Die();
}
}
Upvotes: 1