\\Car Engine Sound Code (Unity5)//
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[i].motorTorque = accelerate * maxTorque;
}
}
void UpdateMeshesPositions()
{
for(int i = 0; i < 4; i++)
{
Quaternion quat;
Vector3 pos;
wheelColliders[i].GetWorldPose(out pos, out quat);
tireMeshes[i].position = pos;
tireMeshes[i].rotation = quat;
}
}
}
i don't know coding so i will appreciate it to someone help me :)
thank you
Do you only want the sound part? Or do you want the steering too?
steering is fine..now i need gears for engine sound, skidding im currently working on car physics for mobile..just for test :D *#%! reflection prob works great! on my windowsPhone 8.1
Answer by Wolfdog · Sep 15, 2015 at 10:43 AM
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.
how can i make sound depend on engine torque. not speed. because if you fall down from somewhere pitch will go up. and if you press brake and gas together your pitch should go up but speed is 0.
Hey it's working but don't know why it's playing sound only the first time my car goes 0-top speed after that when it again comes to deceleration or acceleration it doesn't plays ;_; @Wolfdog
Answer by tayebfaki · Jan 14, 2016 at 01:15 PM
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;
}
Your answer
Follow this Question
Related Questions
Alternate Physics Model Driving Aids 0 Answers
How to fix error in Car Tutorial 0 Answers
MotorTorque reversing not Working 0 Answers
unity 5 car tutorial 3D 1 Answer
I can't import a sky-car,I cannot import the basic sky-car. 0 Answers