- Home /
3rd person character attack wont play sound??HELP!
hey im pritty new to unity, ive gone through the Lerps 3rd person tutorial,from an animation background...so scripting uber noob..
anyway..ive finished the tutorial,but i cant get my "punch" sound to play..one audio listner is attached to near cam, the jetpack plays audio ingame, but the 3rd person attack script does not play the punch sound, with or without a new audio source script attached to the player component.
if the check box is ticked play in awake..then you can hear the punch play once on awake, but not while punching...if unchecked then the sound does not play at all. 2d or 3d sound.
sounds works perfectly for Copper enemies!? have not changed the script from default Lerps tutorial. third person staus script audio works fine(struck sound ,death sound).. the third person character attack script will not play for some reason.(Punch Sound)
any suggestions?
here's the 3rd person character attack.js just in case...
var punchSpeed = 1;
var punchHitTime = 0.2;
var punchTime = 0.4;
var punchPosition = new Vector3 (0, 0, 0.8);
var punchRadius = 1.3;
var punchHitPoints = 1;
var punchSound : AudioClip;
private var busy = false;
function Start ()
{
animation["punch"].speed = punchSpeed;
}
function Update ()
{
var controller : ThirdPersonController = GetComponent(ThirdPersonController);
if(!busy && Input.GetButtonDown ("Fire1") && controller.IsGroundedWithTimeout() && !controller.IsMoving())
{
SendMessage ("DidPunch");
busy = true;
}
}
function DidPunch ()
{
animation.CrossFadeQueued("punch", 0.1, QueueMode.PlayNow);
yield WaitForSeconds(punchHitTime);
var pos = transform.TransformPoint(punchPosition);
var enemies : GameObject[] = GameObject.FindGameObjectsWithTag("Enemy");
for (var go : GameObject in enemies)
{
var enemy = go.GetComponent(EnemyDamage);
if (enemy == null)
continue;
if (Vector3.Distance(enemy.transform.position, pos) < punchRadius)
{
enemy.SendMessage("ApplyDamage", punchHitPoints);
// Play sound.
if (punchSound)
audio.PlayOneShot(punchSound);
}
}
yield WaitForSeconds(punchTime - punchHitTime);
busy = false;
}
function OnDrawGizmosSelected ()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere (transform.TransformPoint(punchPosition), punchRadius);
}
@script RequireComponent(AudioSource)
Answer by bificommander · Aug 03, 2012 at 12:20 PM
I had the same problem and looked around. I found a solution someone else found. Change
if (punchSound) audio.PlayOneShot(punchSound);
to
if (punchSound) AudioSource.PlayClipAtPoint(punchSound, transform.position);
This fixes the problem. But I don't understand the problem. In the EnemyDamage.js script in the same tutorial, the code to play a sound when hit is handled in the same way.
function ApplyDamage (damage : int) { // we've been hit, so play the 'struck' sound. This should be a metallic 'clang'. if (audio && struckSound) audio.PlayOneShot(struckSound); }
This works fine. So the PlayOneShot isn't broken. I looked in the help file and it says PlayOneShot does this:
@script RequireComponent(AudioSource) // Play impact audio clip when colliding with something var impact : AudioClip; function OnCollisionEnter () { audio.PlayOneShot(impact); }
My guess is that when you do a punch, you're not technically colliding with the enemy, and PlayOneShot doesn't know where in the scene the sound is supposed to originate.
Answer by fafase · Aug 03, 2012 at 12:29 PM
Could you try to add a script to your fist or arm object. Instead of checking the distance you can use the physics engine.
You could add a audio source to your fist and:
var punch :AudioClip;
function OnCollisionEnter(other:Collision){
if(other.gameObject.tag =="Enemy")audio.Play();
}
On top of that your function for the punch looks really heavy as you are searching all the enemy and checking which one is involved. With OnCollision you do not have to do all that.
Your answer
![](https://koobas.hobune.stream/wayback/20220613073630im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Third Person Controller Issues With Attack Animations NOT Being Found!? 2 Answers
RPG scripts 2 Answers
How can i make my player roll forward? 2 Answers
Third person controller 1 Answer
Pause Menu Problems 2 Answers