# Only Play Music When Player Is Moving

**URL:** <https://forums.unrealengine.com/t/only-play-music-when-player-is-moving/471446>\
**Category:** Audio\
**Tags:** unreal-engine, question\
**Created:** [September 16, 2020, 4:26pm UTC](https://forums.unrealengine.com/t/only-play-music-when-player-is-moving/471446 "2020-09-16T16:26:26Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![almost.somebody](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@almost.somebody](https://forums.unrealengine.com/u/almost.somebody)\
**Post date:** [September 16, 2020, 4:26pm UTC](https://forums.unrealengine.com/t/only-play-music-when-player-is-moving/471446/1 "2020-09-16T16:26:26Z")

</div>

My idea is as follows: This game is a stage for my next album, and I only want music playing when the player is moving in the stage. This means I need to keep track of where the user is in the song at all times, and constantly check if there is movement. Is there a right way to do this without using a ton of CPU and RAM? I want to make this system as non-destructive as possible so people with low-end computers can play this game as well.

---

<div class="post-metadata">

**Author:** ![EvilCleric](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@EvilCleric](https://forums.unrealengine.com/u/EvilCleric)\
**Post date:** [September 16, 2020, 4:46pm UTC](https://forums.unrealengine.com/t/only-play-music-when-player-is-moving/471446/2 "2020-09-16T16:46:59Z")

</div>

Check velocity and pause/resume the music accordingly.

---

<div class="post-metadata">

**Author:** ![almost.somebody](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@almost.somebody](https://forums.unrealengine.com/u/almost.somebody)\
**Post date:** [September 16, 2020, 4:49pm UTC](https://forums.unrealengine.com/t/only-play-music-when-player-is-moving/471446/3 "2020-09-16T16:49:00Z")

</div>

If I pause it in the blueprint, will it save the current position? Is there even a way to do that?

I just started yesterday, learning the ropes. It doesn’t seem like many people have asked this, or there haven’t been clear answers to my question.

---

<div class="post-metadata">

**Author:** ![DonBusso](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DonBusso](https://forums.unrealengine.com/u/DonBusso)\
**Post date:** [September 16, 2020, 4:53pm UTC](https://forums.unrealengine.com/t/only-play-music-when-player-is-moving/471446/4 "2020-09-16T16:53:07Z")

</div>

If you want to pause/resume song/audio based on movement, you can use a simple function to do that. I put one together quick time. Song starts to play when game starts, if you’re moving it keeps playing if you stop - music pauses and when you start moving again audio is resumed. Of course it’s very crude. Let me know if that is what you meant. It might be a starting point for you. 🙂

---

<div class="post-metadata">

**Author:** ![almost.somebody](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@almost.somebody](https://forums.unrealengine.com/u/almost.somebody)\
**Post date:** [September 16, 2020, 4:55pm UTC](https://forums.unrealengine.com/t/only-play-music-when-player-is-moving/471446/5 "2020-09-16T16:55:57Z")

</div>

Yeah I think I got a basic idea of how to do it- can I automatically assign a value to a variable when the game starts and change it when the branch is called in this example? (the code I’m not showing checks if the player is gaining velocity every tick)

 ![313833-screen-shot-2020-09-16-at-125510-pm.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/f/a/dfa49d4ca81dc24671babda26c5c0bf53853481f.png)

---

<div class="post-metadata">

**Author:** ![almost.somebody](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@almost.somebody](https://forums.unrealengine.com/u/almost.somebody)\
**Post date:** [September 16, 2020, 4:58pm UTC](https://forums.unrealengine.com/t/only-play-music-when-player-is-moving/471446/6 "2020-09-16T16:58:20Z")

</div>

I think I got it after some tinkering, I haven’t tested it yet, will update thread when I run it

---

<div class="post-metadata">

**Author:** ![DonBusso](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DonBusso](https://forums.unrealengine.com/u/DonBusso)\
**Post date:** [September 16, 2020, 5:02pm UTC](https://forums.unrealengine.com/t/only-play-music-when-player-is-moving/471446/7 "2020-09-16T17:02:45Z")

</div>

I think you got it. Just for the sake of it I will give you my setup. It’s done inside a level blueprint. I put up a screenshot if you don’t know how to get there, but it can be done from anywhere really.

 ![313835-level-bp2.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/8/4/f84c3f692cc03ac5661029d2e40bf55167957e83.png)

 ![313836-level-bp.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/5/1/651a309c2fc05a103baba44dc3870b6bf1d5f794.png)

 ![313838-function-levelbp.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/4/8/74836cea690827fca5654191e28b68eef4b79cba.png)

---

<div class="post-metadata">

**Author:** ![DonBusso](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DonBusso](https://forums.unrealengine.com/u/DonBusso)\
**Post date:** [September 16, 2020, 5:25pm UTC](https://forums.unrealengine.com/t/only-play-music-when-player-is-moving/471446/8 "2020-09-16T17:25:18Z")

</div>

The only problem you might have now is when music finishes, then you will get errors. I couldn’t get the music to play again using On Audio Finished event from Spawned Sound but here’s a work around. This will stop the errors.

 ![313854-updated-graph.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/f/c/afc09b96c73ef213fed6501cf11fb3c4958f6f86.png)

 ![313855-updated-function.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/b/7/eb7c62dc40e236660897bfdaffbbf07696faa1a8.png)

---

<div class="post-metadata">

**Author:** ![almost.somebody](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@almost.somebody](https://forums.unrealengine.com/u/almost.somebody)\
**Post date:** [September 16, 2020, 6:38pm UTC](https://forums.unrealengine.com/t/only-play-music-when-player-is-moving/471446/9 "2020-09-16T18:38:10Z")

</div>

Is there a way to make it play different audio files when this one ends? And then when it’s out of audio files can it stop playing music?

---

<div class="post-metadata">

**Author:** ![almost.somebody](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@almost.somebody](https://forums.unrealengine.com/u/almost.somebody)\
**Post date:** [September 16, 2020, 6:38pm UTC](https://forums.unrealengine.com/t/only-play-music-when-player-is-moving/471446/10 "2020-09-16T18:38:49Z")

</div>

I’m very new to unreal, actually, I just started yesterday on this project. I plan to be done with this game in 4 months, so any help is greatly appreciated 😛

---

<div class="post-metadata">

**Author:** ![DonBusso](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DonBusso](https://forums.unrealengine.com/u/DonBusso)\
**Post date:** [September 16, 2020, 6:49pm UTC](https://forums.unrealengine.com/t/only-play-music-when-player-is-moving/471446/11 "2020-09-16T18:49:21Z")

</div>

It’s quite alright, I’m not that advanced myself (self-learning). It comes with time. I was just thinking about music for my own game and you could setup a array of music files and then just fish them out of there one by one. You could randomize it that way too.  
I will put together a very basic setup and post it here.

---

<div class="post-metadata">

**Author:** ![DonBusso](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DonBusso](https://forums.unrealengine.com/u/DonBusso)\
**Post date:** [September 16, 2020, 7:15pm UTC](https://forums.unrealengine.com/t/only-play-music-when-player-is-moving/471446/12 "2020-09-16T19:15:05Z")

</div>

Just a simple pulling out of array. I’m not showing here how to randomize audio files or make sure you don’t play the same ones over and over and such. This isn’t that hard to do so you can give it a go.

 ![313875-graph-warray.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/c/b/2cb2c980efb32f08f1e2ddd2f0c533336fd6f4fa.png)

 ![313876-function-warray.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/e/a/fea37db5c7ff022f2f0af85a3f2f3eb2c19f695f.png)
