Forum rules - please read before posting.

Adding dragging sound to the GameCamera2DDrag script

Hello,

I'm want to add a dragging sound to this script:

https://adventurecreator.org/scripting-guide/class_a_c_1_1_game_camera2_d_drag.html

The idea is that it's akin to a fake 2D first person perspective. The player is looking through one of those viewfinder things you find at parks overlooking a beautiful horizon. Kind of like this:

https://www.istockphoto.com/search/2/image?phrase=viewfinder+view+finder+binoculars+looking+through+window

The script works great. I just want to add sound to the script so that... when Camera is moving, the looped sound plays. And when the camera stops, the looped sound stops. I guess it could make use of sound like with the draggables on tracks. Whichever is easiest.

Personally, I just looked at the GameCamera2DDrag.cs script and couldn't figure out where to put that logic.

Any help with this would be appreciated. Thanks!

Comments

  • edited April 2023

    You can attach a custom script that plays the sound separately - no need to modify the original script:

    using UnityEngine;
    using AC;
    
    public class CameraDragSound : MonoBehaviour
    {
    
        public float minThreshold = 0.1f;
        public AudioSource dragAudioSource;
    
        void Update ()
        {
            if (KickStarter.playerInput.GetDragState () == DragState._Camera && KickStarter.playerInput.GetDragVector ().magnitude > minThreshold)
            {
                dragAudioSource.Play ();
            }
            else
            {
                dragAudioSource.Stop ();
            }
        }
    
    }
    
  • edited April 2023

    Thanks, Chris! This script kind of works... one thing, I had to add an "f":

    public float minThreshold = 0.1;

    But there is another issue -- it might be because it's a "void update" and it's playing every frame, but it sounds like this:

    https://www.loom.com/share/819d929a4c734b5a87d544022f760b6f

    What do you think we can do to remedy this? I noticed that the logic is quite complex on Moveable_Drag.cs and DragBase.cs, with the PlayMoveSound method on the latter. Do you think we should add something like that to this script?

    Let me know. Thanks!

  • Here's one that adapts that function - you'll need to attach a Sound component as well:

    using UnityEngine;
    using AC;
    
    public class CameraDragSound : MonoBehaviour
    {
    
        public float minThreshold = 0.1f;
        public float slidePitchFactor = 1f;
        public Sound moveSound;
    
        void Update ()
        {
            float dragSpeed = (KickStarter.playerInput.GetDragState () == DragState._Camera)
                             ? KickStarter.playerInput.GetDragVector ().magnitude
                             : 0f;
            PlayMoveSound (dragSpeed);
        }
    
        protected void PlayMoveSound (float speed)
        {
            if (slidePitchFactor > 0f)
            {
                float targetPitch = Mathf.Min (1f, speed * slidePitchFactor);
                moveSound.audioSource.pitch = Mathf.Lerp (moveSound.audioSource.pitch, targetPitch, Time.deltaTime * 3f);
            }
    
            if (speed > minThreshold)
            {
                if (!moveSound.IsPlaying ())
                {
                    moveSound.Play (true);
                }
            }
            else if (moveSound.IsPlaying () && !moveSound.IsFading ())
            {
                moveSound.FadeOut (0.2f);
            }
        }
    
    }
    
  • Works perfectly. You are the best. Thanks!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.