Search Unity

Tyre slip audio problem

Discussion in 'Scripting' started by Deleted User, Jul 16, 2018.

  1. Deleted User

    Deleted User

    Guest

    Tried a couple of solutions, but can't get my skid audio to play correctly without errors, would appreciate any pointers, basic outline code below :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class skiddy : MonoBehaviour {
    6.  
    7.     [Header("Skid Audio")]
    8.     public AudioClip skidAudio;
    9.  
    10.     private void Start()
    11.     {
    12.         AudioSource source = gameObject.AddComponent<AudioSource>();    // Create AudioSource
    13.         source.clip = skidAudio;                                        // Load clip into AudioSource
    14.     }
    15.  
    16.     void Update () {
    17.         WheelHit hit;
    18.         WheelCollider collider;
    19.         collider = GetComponent<WheelCollider>();
    20.         collider.GetGroundHit(out hit);
    21.  
    22.         if (hit.sidewaysSlip > 0.5f) {          
    23.             Debug.Log("Wheel Slipping");
    24.  
    25.             //source.Play();
    26.             //Destroy(source);                                                // Destroy AudioSource when done
    27.         }
    28.     }
    29. }
     
  2. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    You need to be more specific - What errors were you getting?
    I would assume it would be some kind of NullReferenceException. You may need to change:
    Code (CSharp):
    1. if (hit.sidewaysSlip > 0.5f)
    To:
    Code (CSharp):
    1. if (collider.isGrounded && hit.sidewaysSlip > 0.5f)
     
  3. Deleted User

    Deleted User

    Guest

    Yeah, sorry, I meant for some reason, I can't get my sound to play correctly once the condition is met, the variants in code I tried meant I was creating 'lots' of audiosources on the fly ( super slowdown occurs and horribly jerky audio ), or playing once and then never again.

    On a secondary note, I also have this version :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class skidSounds : MonoBehaviour {
    6.  
    7.     private float currentFrictionValue = 0f;
    8.     private float SkidAt = 0.5f;
    9.     private float soundEmission = 5f;
    10.     private float soundWait = 15f;
    11.  
    12.     GameObject skidSound;
    13.  
    14.     private void Update()
    15.     {
    16.         playerSkidSound();
    17.     }
    18.  
    19.     private void playerSkidSound()
    20.     {
    21.         WheelHit hit;
    22.         WheelCollider collider;
    23.         collider = GetComponent<WheelCollider>();
    24.         collider.GetGroundHit(out hit);
    25.  
    26.         currentFrictionValue = Mathf.Abs(hit.sidewaysSlip);
    27.  
    28.         if (SkidAt <= currentFrictionValue && soundWait <= 0) {
    29.             skidSound = Instantiate(Resources.Load("soundSlip"), hit.point, Quaternion.identity) as GameObject;
    30.             soundWait = 1;
    31.         }
    32.  
    33.         soundWait -= Time.deltaTime * soundEmission;
    34.     }
    35. }
    together with :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class skidDestroy : MonoBehaviour {
    6.  
    7.     private float destroyAfter = 2f;
    8.     private float timer = 0f;
    9.  
    10.     void Update () {
    11.         timer += Time.deltaTime;
    12.         if(destroyAfter <= timer) {
    13.             Destroy(gameObject);
    14.         }
    15.     }
    16. }
    but seems a pretty 'long' way of doing the same code, I don't want to instantiate a prefab in each time, especially via Resources.Load, if I can just load my skidmark sound in as an audioclip and attach it to an audiosource right in the code instead ? I essentially want to do the above, but in the 'way' of the first script posted.
     
  4. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    The way you're doing this is not very efficient. Instead of instantiating an AudioSource everytime you skid, a better way to do it would be to have a single AudioSource playing the skid sound on loop, then adjust its volume based on the sideways slip. The "~AlternatePhysicsModel" from the old Unity 3.x car tutorial does it this way, and I thought it was a much better, and more optimised way of doing skid sounds.

    Old car tutorial link:
    https://assetstore.unity.com/packages/templates/tutorials/car-tutorial-unity-3-x-only-10
     
  5. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    451
    One thing: Don't use Instantiate and GetComponent in Update. You will run into performance and GC problems. Also you can assign WeelHit and WheelCollider once as they are used in every frame.
    If you are trying to make a wheel squeek why not add a audio source with the clip in inspector to your wheel or the vehicle and play looped once the conditions are met and stop it as soon as they aren't anymore?