Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by MohammadAlizadeh · Sep 15, 2015 at 06:43 AM · car-tutorial

\\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 alt text

gameplayonlumia520.jpg (51.7 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MakakWasTaken · Sep 15, 2015 at 10:14 AM 0
Share

Do you only want the sound part? Or do you want the steering too?

avatar image MohammadAlizadeh MakakWasTaken · Sep 16, 2015 at 06:20 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MohammadAlizadeh · Sep 15, 2015 at 11:08 AM 0
Share

Thanks.. it's super simple though lol

avatar image blackshtormx · Nov 07, 2016 at 04:50 PM 0
Share

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.

avatar image RACHITSINGH433 · Jan 19, 2019 at 02:38 AM 0
Share

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

avatar image
0

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;



 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges