Hi

i want a engine sound code to give more realism to my car

here’s a code that i took from a tutorial:

using UnityEngine;
using System.Collections;

public class Car : MonoBehaviour
{
    public float maxTorque = 50f;

    public Transform centerOfMass;

    public WheelCollider[] wheelColliders = new WheelCollider[4];
    public Transform[] tireMeshes = new Transform[4];

    private Rigidbody m_rigidBody;

    void Start()
    {
        m_rigidBody = GetComponent<Rigidbody>();
        m_rigidBody.centerOfMass = centerOfMass.localPosition;
    }

    void Update()
    {
        UpdateMeshesPositions();
    }

    void FixedUpdate()
    {
        float steer = Input.GetAxis("Horizontal");
        float accelerate = Input.GetAxis("Vertical");

        float finalAngle = steer * 45f;
        wheelColliders[0].steerAngle = finalAngle;
        wheelColliders[1].steerAngle = finalAngle;

        for(int i = 0; i < 4; i++)
        {
            wheelColliders_.motorTorque = accelerate * maxTorque;_

}
}

void UpdateMeshesPositions()
{
for(int i = 0; i < 4; i++)
{
Quaternion quat;
Vector3 pos;
wheelColliders*.GetWorldPose(out pos, out quat);*

tireMeshes*.position = pos;*
tireMeshes*.rotation = quat;*
}
}
}
i don’t know coding so i will appreciate it to someone help me :slight_smile:
thank you
[54395-gameplayonlumia520.jpg|54395]

Hi, I will try to point you in the right direction.

You need a public float topSpeed variable (and set it to your top speed) and a float currentSpeed variable to hold the current speed.
Set the currentSpeed variable to your car’s speed every frame (in the Update() function).

You also need to have an AudioSource component on your object with the audio clip of your car’s engine. Create a new variable called float pitch.
set the pitch = currentSpeed / topSpeed every frame to get a pitch relative to your car’s speed.

Then, at the end of the update function, set the transform.GetComponent <AudioSource> ().Pitch = pitch;

This should change the pitch of the engine as you accelerate or decelerate.

Here is a code example:

public float topSpeed = 100; // km per hour
private float currentSpeed = 0;
private float pitch = 0;

void Update () {
    currentSpeed = transform.GetComponent <Rigidbody> ().velocity.magnitude * 3.6f;
    pitch = currentSpeed / topSpeed;

    transform.GetComponent <AudioSource> ().Pitch = pitch;
}

You might want to cache the rigidbody and audiosource if you want better performance, however this will do fine for a test.

This code works correctly with a simple modification
Thank you

using UnityEngine;
using System.Collections;

public class Car : MonoBehaviour
{
public float maxTorque = 50f;

public Transform centerOfMass;

public WheelCollider[] wheelColliders = new WheelCollider[4];
public Transform[] tireMeshes = new Transform[4];

private Rigidbody m_rigidBody;
public float topSpeed = 100; // km per hour
private float currentSpeed = 0;
private float pitch = 0;

void Start()
{
    m_rigidBody = GetComponent<Rigidbody>();
    m_rigidBody.centerOfMass = centerOfMass.localPosition;
}

void Update()
{
    UpdateMeshesPositions();
    currentSpeed = transform.GetComponent<Rigidbody>().velocity.magnitude * 3.6f;
    pitch = currentSpeed / topSpeed;

    GetComponent<AudioSource>().pitch = pitch;

}