• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by kazamaninja · Jul 03, 2012 at 03:42 PM · audioattackpersonthirdpunch

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)
Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image

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.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

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.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Third Person Controller Issues With Attack Animations NOT Being Found!? 2 Answers

Third Person Shooter 2 Answers

ThirdPerson Unity Networking? 0 Answers

How to shoot exactly where mouse is (third person) 1 Answer

Best way to handle 2.5d punch attack? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges