Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Car Engine Sound Based On Motor Torque

Discussion in 'Scripting' started by playmint, Jul 14, 2018.

  1. playmint

    playmint

    Joined:
    Jan 11, 2014
    Posts:
    287
    Hi,

    I'm creating a simple vehicle controller for a small game I'm working on, and I'm currently implementing the sound code for the engine when the car increases in speed. I have the following reference that I've studied and implemented with no problem ( see answer ) : -

    https://answers.unity.com/questions/1067016/car-engine-sound-code-unity5car-engine-sound-code.html

    However, I would like to have my car engine sound based on the actual Motor Torque, would appreciate any input on how I might amend my current code ( based on the help given above ) to achieve this ?

    Thanks
     
  2. playmint

    playmint

    Joined:
    Jan 11, 2014
    Posts:
    287
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class vehicleSounds : MonoBehaviour {
    6.  
    7.     public float topSpeed = 100f;
    8.  
    9.     private float currentSpeed = 0;
    10.     private float pitch = 0;
    11.    
    12.     void Update () {
    13.         currentSpeed = transform.GetComponent<Rigidbody>().velocity.magnitude * 3.6f;
    14.  
    15.         pitch = currentSpeed / topSpeed;
    16.         GetComponent<AudioSource>().pitch = pitch;
    17.  
    18.         //Debug.Log(currentSpeed); // Showcase current speed
    19.     }
    20. }
    21.  
    So, this is what I'm currently doing, would appreciate the help ?
     
  3. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,414
    Create an Animation Curve where x would be the torque and y would be the pitch.

    Use the Evaluate function to get the pitch at the given torque and pass that over to your AudioSource.
     
  4. playmint

    playmint

    Joined:
    Jan 11, 2014
    Posts:
    287
    Oh, wow, I think that might be a bit more advanced than what I was looking for, is there a simpler method ?
     
  5. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,414
    You could just use the current code you have, but replace current speed with the current motor torque and top speed with the maximum motor torque. I guess that would be a simpler way.

    Also, engine sound is usually calculated from the engine RPM, not the torque. This post may help you out:

    http://entitycrisis.blogspot.com/2010/12/unity3d-wheelcollider-and-motortorque.html
     
    playmint and Avietry like this.