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 Hinge door sounds

Discussion in 'Scripting' started by Atix07, Aug 9, 2021.

  1. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    Hello!

    I did create hinge doors with rigidbody. Im adding force accoring to mouse input but Im stuggling to add sound effects to door. Since I cant understand if it is open or not so I cant add sound effects. İf I check the angle of the door like example door local angle.z == 0 it will call always. Opening door sound effects is way harder. How can I handle this?

    This is the door controller on my player:
    Code (CSharp):
    1. public void doorSelectedd(GameObject doorObject)
    2.     {
    3.         selectedDoor = doorObject;
    4.         info = selectedDoor.GetComponent<objectInfo>();
    5.  
    6.         if (!info.pv.AmOwner)
    7.             info.pv.TransferOwnership(PhotonNetwork.LocalPlayer);
    8.        
    9.         usingDoor = true;
    10.         info.Objectrigidbody.drag = 2;
    11.     }
    12.  
    13.     void Update ()
    14.     {
    15.         if (!pv.IsMine)
    16.             return;
    17.  
    18.         if (selectedDoor == null)
    19.             return;    
    20.  
    21.         if (usingDoor && Input.GetKeyUp(KeyCode.Mouse0) || usingDoor && Input.GetKeyUp(KeyCode.Joystick1Button5))
    22.         {
    23.             info.Objectrigidbody.drag = 10;
    24.             usingDoor = false;
    25.             selectedDoor = null;
    26.         }
    27.         else if (selectedDoor != null && Vector3.Distance(this.gameObject.transform.position, selectedDoor.transform.position) > selection.rayRange + 1)
    28.         {
    29.             info.Objectrigidbody.drag = 10;
    30.             usingDoor = false;
    31.             selectedDoor = null;
    32.         }
    33.     }
    If using door is true Im enabling add force stuff in fixed update.

    edit**: I tried making a scipt called door in door prefab checking local angle but failed

    Thanks! I would like to hear any advice.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Just make an open-door animation and a close-door animation, play them in response to the proper user input. When you play them, start a correlating sound playing. Far less code required.
     
  3. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    Thanks for answer!

    Here is the video of the door:


    So Im moving the door with this code. Really classical horror game door logic. So I cant really make it with animations:
    Code (CSharp):
    1. private void FixedUpdate()
    2.     {
    3.         if (usingDoor)
    4.         {
    5.             if (Input.mousePresent)
    6.             {
    7.                 if (Input.GetAxis("Mouse X") > 0)
    8.                 {
    9.                     info.Objectrigidbody.AddForce(gameObject.transform.right * (Input.GetAxis("Mouse X")) * force);
    10.                 }
    11.                 else if (Input.GetAxis("Mouse X") < 0)
    12.                 {
    13.                     info.Objectrigidbody.AddForce(-gameObject.transform.right * (-Input.GetAxis("Mouse X")) * force);
    14.                 }
    15.  
    16.                 if (Input.GetAxis("Mouse Y") > 0)
    17.                 {
    18.                     info.Objectrigidbody.AddForce(gameObject.transform.forward * (Input.GetAxis("Mouse Y")) * force);
    19.                 }
    20.                 else if (Input.GetAxis("Mouse Y") < 0)
    21.                 {
    22.                     info.Objectrigidbody.AddForce(-gameObject.transform.forward * (-Input.GetAxis("Mouse Y")) * force);
    23.                 }
    24.             }
    25.             else
    26.             {
    27.                 if (Input.GetAxis("Right Stick Horizontal") > 0)
    28.                 {
    29.                     info.Objectrigidbody.AddForce(gameObject.transform.right * (Input.GetAxis("Right Stick Horizontal")) * force);
    30.                 }
    31.                 else if (Input.GetAxis("Right Stick Horizontal") < 0)
    32.                 {
    33.                     info.Objectrigidbody.AddForce(-gameObject.transform.right * (-Input.GetAxis("Right Stick Horizontal")) * force);
    34.                 }
    35.  
    36.                 if (Input.GetAxis("Right Stick Vertical") > 0)
    37.                 {
    38.                     info.Objectrigidbody.AddForce(gameObject.transform.forward * (Input.GetAxis("Right Stick Vertical")) * force);
    39.                 }
    40.                 else if (Input.GetAxis("Right Stick Vertical") < 0)
    41.                 {
    42.                     info.Objectrigidbody.AddForce(-gameObject.transform.forward * (-Input.GetAxis("Right Stick Vertical")) * force);
    43.                 }
    44.             }
    45.         }
    46.     }
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    Perhaps you could try adjusting the pitch of the audio source based on the angular velocity of the door's rigid body?

    When the door is opened you start playing the audio source, and in the door's update loop, it checks its angular velocity and sets the audio's pitch.
     
    Atix07 likes this.
  5. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    Thank you. Probably its best way to do it.