Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    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. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    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:
    279
    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:
    279
    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. Intense_Gamer94

    Intense_Gamer94

    Joined:
    Jun 20, 2013
    Posts:
    1,366
    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:
    279
    Oh, wow, I think that might be a bit more advanced than what I was looking for, is there a simpler method ?
     
  5. Intense_Gamer94

    Intense_Gamer94

    Joined:
    Jun 20, 2013
    Posts:
    1,366
    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.