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

play sound when move a object

Discussion in 'Scripting' started by jaimemejias1995, Sep 11, 2021.

  1. jaimemejias1995

    jaimemejias1995

    Joined:
    Mar 6, 2021
    Posts:
    3
    Hello!
    I'm beginner on scripting.

    I have a door with a hinge joint, and i need a script to play a sound when i move the door.
    Please, someone can helps me??
    Really thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    It is already all waiting for you to enjoy right here:

    Screen Shot 2021-09-11 at 11.46.17 AM.png
     
    jaimemejias1995 likes this.
  3. jaimemejias1995

    jaimemejias1995

    Joined:
    Mar 6, 2021
    Posts:
    3
    Yes but i dont found a script for play with movement :(
     
  4. RadioactiveChickenGames1

    RadioactiveChickenGames1

    Joined:
    Sep 29, 2020
    Posts:
    15
    Are you trying to play a sound upon the object moving, or are you trying to play a sound and move the object in one script?
     
    jaimemejias1995 likes this.
  5. jaimemejias1995

    jaimemejias1995

    Joined:
    Mar 6, 2021
    Posts:
    3
    I only need a script to play a sound when the rotation of object change. The door has a rigibody and a hinge joint for move by physics.
     
  6. RadioactiveChickenGames1

    RadioactiveChickenGames1

    Joined:
    Sep 29, 2020
    Posts:
    15
    There are two ways you could do this. One is to put this code on the door:
    Code (CSharp):
    1. public Quaternion recentRotation;
    2. if (recentRotation != transform.rotation) {
    3.     //play sound
    4. }
    5. recentRotation = transform.rotation;
    But not exactly; you'd need to change it. Basically what it does is it senses when the door's rotation changes with a temporary variable. It's not all that efficient, though. A better way would be to make the script that moves the door play the sound.