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. Dismiss Notice

Question How to make sound play on keypress

Discussion in 'Audio & Video' started by itsdandd, Apr 10, 2023.

  1. itsdandd

    itsdandd

    Joined:
    Jan 6, 2020
    Posts:
    44
    Hello and thank you for taking some time to read this!
    I am making a simple car game and I don't know how to make a sound play when a key is pressed.
    What I would like to make is a basic car idling sound looping then when the up arrow is press it fades into a engine running at a higher rpm.
    Maybe there is a better way of doing this but its all I could think of.

    Thank you for your time!
     
  2. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    638
    Thats a little more complicated then just simply 'when a button is pressed'.

    You will want to consider looking into an engine revolution looping clip, and controlling the pitch of these loops over time based on rpms, etc.

    Home work time.

    PS: Or. you can do like me and Buy Edys Vehicle Phys. Takes care of all the stuffs. lol. Theres other compareables out there. GL
     
  3. itsdandd

    itsdandd

    Joined:
    Jan 6, 2020
    Posts:
    44
    Thanks for the reply!
    I figured it would be much more complicated then I thought it would be.
    I am very new to unity so I don’t really have a good understanding in how much of this works yet.
    I will do more research and try to find something else.
    What I am working on isn’t worth spending money on.

    Thanks again
     
  4. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    261
    Something you could do is to play your looping sound and pitch it up and down with Mathf.MoveTowards.

    Code (CSharp):
    1. [RequireComponent(typeof(AudioSource))]
    2. public class LoopingPitchedSound : MonoBehaviour
    3. {
    4.     [SerializeField] private AudioClip soundClip;
    5.     [SerializeField] private float minPitch = 0.5f;
    6.     [SerializeField] private float maxPitch = 1.5f;
    7.     [SerializeField] private float pitchChangeSpeed = 0.5f;
    8.     [SerializeField] private float anchorPitch = 1f;
    9.  
    10.     private AudioSource audioSource;
    11.  
    12.     private void Awake()
    13.     {
    14.         audioSource = GetComponent<AudioSource>();
    15.     }
    16.  
    17.     private void Start()
    18.     {
    19.         audioSource.clip = soundClip;
    20.         audioSource.loop = true;
    21.         audioSource.Play();
    22.     }
    23.  
    24.     private void Update()
    25.     {
    26.         if (Input.GetKey(KeyCode.UpArrow))
    27.         {
    28.             audioSource.pitch = Mathf.MoveTowards(audioSource.pitch, maxPitch, pitchChangeSpeed * Time.deltaTime);
    29.         }
    30.         else if (Input.GetKey(KeyCode.DownArrow))
    31.         {
    32.             audioSource.pitch = Mathf.MoveTowards(audioSource.pitch, minPitch, pitchChangeSpeed * Time.deltaTime);
    33.         }
    34.         else
    35.         {
    36.             audioSource.pitch = Mathf.MoveTowards(audioSource.pitch, anchorPitch, pitchChangeSpeed * Time.deltaTime);
    37.         }
    38.     }
    39. }
     
    itsdandd likes this.
  5. itsdandd

    itsdandd

    Joined:
    Jan 6, 2020
    Posts:
    44
    That sounds like it stands a chance of working the only issue that stands between me and trying it is that I don't know how to implement this.

    Thanks for your reply!
     
  6. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    261
    1. Create a script component with the code there
    2. Add the component to your vehicle GameObject and fill its values in the inspector
    3. Press Play and try it out to see if it does what you wish
    4. Press Stop because it won't
    5. Doodle, try stuff, enjoy Unity and come back here if you need help ;)
    6. Repeat from step 3 until you're satisfied
     
    itsdandd likes this.
  7. itsdandd

    itsdandd

    Joined:
    Jan 6, 2020
    Posts:
    44
    I have tried to get this to work with my limited knowledge and I still am having no luck.
    All I am getting is plagued with errors.
    I am not sure what to fill in where.
    This is the last thing to do with my project and its proving to be the hardest.
     
  8. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    261
    What kind of errors? Let's have a look at that first and then if it's possible/relevant, please share some of your code and video examples of the gameplay bugs. We'll see what we can do!
     
    itsdandd likes this.
  9. itsdandd

    itsdandd

    Joined:
    Jan 6, 2020
    Posts:
    44
    For the vehicle/controller script I am using everything from this tutorial:


    The only errors I am getting are from the code you gave me I think.

    I didn't expect for the code you gave me to just to work perfectly I figured I would have to change something but I didn't know what to try to do.

    Here are the errors am getting:
    upload_2023-4-30_15-4-57.png
    upload_2023-4-30_15-5-34.png

    Thanks again for taking the time out to help me!
     
  10. icauroboros

    icauroboros

    Joined:
    Apr 30, 2021
    Posts:
    96
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    These two lines need to be exist on very top of every code file.

    You need make sure understand every concept on this series
    https://learn.unity.com/project/beginner-gameplay-scripting
    https://learn.unity.com/project/intermediate-gameplay-scripting

    If you trying to learn just with fun tutorials and skipping basics, you wasting your time.
    You cannot make games with just copy and paste.
     
    itsdandd likes this.
  11. itsdandd

    itsdandd

    Joined:
    Jan 6, 2020
    Posts:
    44

    That's embarrassing that I missed that but that fixed it.
    Thanks!
    Your right I probably should take a look at the basics like that but I don't have much time these days.
     
    SeventhString and icauroboros like this.