Search Unity

Can't get punch sound to work on Lerpz

Discussion in 'Scripting' started by spiderlizard22, Mar 11, 2012.

  1. spiderlizard22

    spiderlizard22

    Joined:
    Dec 4, 2011
    Posts:
    6
    I am working on a 3D platform tutorial called Lerpz Escapes.I when I press the control button Lerpz punches. That works fine. The problem is when he punches their is no sound. I even put the LerpzPunch in the thirdPersonCharacterAttack script. I checked the code and can't figure out what the problem is. Could anyone help me?

    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");

    if (punchSound)
    audio.PlayOneShot(punchSound);

    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)
     
  2. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Did you put the sfx file in the inspector where its cached at??
     
  3. spiderlizard22

    spiderlizard22

    Joined:
    Dec 4, 2011
    Posts:
    6
    I solved the problem.


    I change it from if (punchSound);
    audio.PlayOneShot(punchSound); to

    if (punchSound)
    AudioSource.PlayClipAtPoint(punchSound, transform.position);





    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");

    if (punchSound)
    AudioSource.PlayClipAtPoint(punchSound, transform.position);

    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);
    }
     
    Last edited: Mar 12, 2012
  4. Zerathule

    Zerathule

    Joined:
    Sep 4, 2012
    Posts:
    2
    That solution actually works for a lot of sound issues I have been having. Namely pick-ups being to quiet, metal hit not activating, and other sound effects that where just acting wonky. People who are having issue with sound in this tutorial should really play around with option and see where it can help fit in.

    Another issue I was having was using the volume controls for 3D space so that sounds would drop off depending on their distance.

    Most of this is due to the fact that since when this tutorial was made, unity has undergone a lot of changes including how sound works. Originally 3D sound in space was not supported.

    As the Stimarco explained in this thread...
    http://forum.unity3d.com/threads/66594-Lerpz-Punch-Sound-problem
     
  5. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    Please use
    Code (csharp):
    1.  tags for your code.
     
  6. Zerathule

    Zerathule

    Joined:
    Sep 4, 2012
    Posts:
    2
     
  7. kanga

    kanga

    Joined:
    Mar 22, 2010
    Posts:
    225
    Dang! Worked for me. Thanks spiderlizard22 :)