Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Car Engine Sound Based On Motor Torque

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

  1. Deleted User

    Deleted User

    Guest

    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. Deleted User

    Deleted User

    Guest

    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,548
    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. Deleted User

    Deleted User

    Guest

    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,548
    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
     
    PrimalCoder, Deleted User and m0guz like this.