Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    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 at 1:27 AM.

  1. playmint

    playmint

    Joined:
    Jan 11, 2014
    Posts:
    262
    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:
    262
    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,305
    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:
    262
    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,305
    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.