A more realistic sound of the vehicle.
Hello! I want to change the sound of the car more realistic.
The current effect is a little boring (sound pitch to be changed with the speed).I searched the Internet solution but have not found anything interesting. Can I use this script can create the effect of shifting? If so, how?
Example sound effect: https://youtu.be/TKNjFv4DRF0?t=48s C# script
using UnityEngine;
using System.Collections;
public class RearWheelDriveXp : MonoBehaviour
{
private WheelCollider[] wheels;
public float maxAngle = 30;
public float maxTorque = 300;
public float topSpeed = 100; // km per hour
public float currentSpeed = 0;
private float pitch = 4f;
public Transform visualCar;
public GameObject CarEngine;
public float Brakes;
public float BrakesForce;
// here we find all the WheelColliders down in the hierarchy
public void Start()
{
wheels = GetComponentsInChildren<WheelCollider>();
GetComponent<Rigidbody>().centerOfMass = visualCar.localPosition;
}
// this is a really simple approach to updating wheels
// here we simulate a rear wheel drive car and assume that the car is perfectly symmetric at local zero
// this helps us to figure our which wheels are front ones and which are rear
public void Update()
{
float angle = maxAngle * Input.GetAxis("Horizontal");
float torque = maxTorque * Input.GetAxis("Vertical");
Brakes = 0;
if (Input.GetKey(KeyCode.Space) == true)
{
Brakes = BrakesForce;
}
foreach (WheelCollider WC in wheels)
{
WC.brakeTorque = Brakes;
}
foreach (WheelCollider wheel in wheels)
{
// a simple car where front wheels steer while rear ones drive
if (wheel.transform.localPosition.z > 0)
wheel.steerAngle = angle;
if (wheel.transform.localPosition.z < 0)
wheel.motorTorque = torque;
// update visual wheels if any
{
Quaternion q;
Vector3 p;
wheel.GetWorldPose(out p, out q);
// assume that the only child of the wheelcollider is the wheel shape
Transform shapeTransform = visualCar.FindChild(wheel.name);
shapeTransform.position = p;
shapeTransform.rotation = q;
}
}
// CURRENTSPEED
currentSpeed = (Mathf.Round(GetComponent<Rigidbody>().velocity.magnitude));
// ENGINE SOUND
pitch = currentSpeed / topSpeed;
CarEngine.GetComponent<AudioSource>().pitch = pitch + 0.5f;
}
public void FixedUpdate()
{
if (currentSpeed > topSpeed)
{
GetComponent<Rigidbody>().velocity = GetComponent<Rigidbody>().velocity.normalized * topSpeed;
}
GetComponent<Rigidbody>().velocity = Vector3.ClampMagnitude(GetComponent<Rigidbody>().velocity, topSpeed);
}
}
Comment
Best Answer
Answer by Mativve · Jan 17, 2017 at 04:43 PM
I import Standard Assets and Replace object car, :)
Your answer
Follow this Question
Related Questions
I want to press a Button and play a sound? 2 Answers
Stopping the car audio when im not in the car 1 Answer
How to make brake car ? 2 Answers
Problem with turn left light 1 Answer
How Add a Sound car in project? 0 Answers