Jump to content

Music depends on speed of player


Timiimit

Recommended Posts

--FUNCTION FROM WIKI: [url=https://wiki.multitheftauto.com/wiki/GetElementSpeed]https://wiki.multitheftauto.com/wiki/GetElementSpeed[/url] 
function getElementSpeed(element,unit) 
    if (unit == nil) then unit = 0 end 
    if (isElement(element)) then 
        local x,y,z = getElementVelocity(element) 
        if (unit=="mph" or unit==1 or unit =='1') then 
            return (x^2 + y^2 + z^2) ^ 0.5 * 100 
        else 
            return (x^2 + y^2 + z^2) ^ 0.5 * 1.61 * 100 
        end 
    else 
        outputDebugString("Not an element. Can't get speed") 
        return false 
    end 
end 
---------------------- 
  
  
--YOUR SCRIPT 
sound = playSound ( "music.mp3", true ) 
setSoundVolume ( sound, 1 ) 
------------- 
  
  
setTimer(function() 
    local speed = getElementSpeed(getLocalPlayer(), 'mph') 
    speed = math.floor(speed) 
    local soundSpeed  
    if speed < 20 then  
        soundSpeed = 1 
    else 
        soundSpeed = speed*0.05 
    end  
    setSoundSpeed(sound, soundSpeed) 
end, 200, 0 ) 

Test this. It's a start for you and you can easily edit it now

Link to comment

Why on earth would you use a timer? -.-

  
local sound = playSound("music.mp3", true) 
  
--This function adjusts the speed of the sound, every render, based on the local player's velocity. 
local function adjustSoundSpeed() 
    --Remove this event handler is the sound is removed 
    if not isElement(sound) then 
        removeEventHandler("onClientRender", root, adjustSoundSpeed) 
        return 
    end 
     
    --Determine whether or not to calculate the speed of the local player, or the vehicle he/she is in. 
    local player = localPlayer 
    if isPedInVehicle(player) then 
        player = getPedOccupiedVehicle(player) 
    end 
     
    --Calculate the velocity of the local player 
    local velX, velY, velZ = getElementVelocity(player) 
    local velocity = (velX^2 + velX^2 + velX^2)^(0.5) 
     
    --Calculate a reasonable speed 
    local speed = --[[ ************* THIS IS UP TO YOU TO DO ****************]] 
    setSoundSpeed(sound, speed) 
end 
addEventHandler("onClientRender", root, adjustSoundSpeed) 
  

That is a better solution. Note, however, that I've left it up to YOU to determine the speed based on the velocity, since you must decide the limits an such. Remember that speed must be between 0 and 1.

Link to comment
  • 1 month later...

thx for everithing!

my finished script:

  
sound = playSound ("nyancat.mp3", true)  
  
local function adjustSoundSpeed() 
    --Remove this event handler is the sound is removed 
    if not isElement(sound) then 
        removeEventHandler("onClientRender", root, adjustSoundSpeed) 
        return 
    end 
    
   
    local player = localPlayer 
    if isPedInVehicle(player) then 
        player = getPedOccupiedVehicle(player) 
    end 
    
    --Calculate the velocity of the local player 
    local velX, velY, velZ = getElementVelocity(player) 
    local velocity = (velX^2 + velY^2 + velZ^2)^(0.5) 
    local speed = velocity / 2 
    
    -- Get the time 
    local hour, minute = getTime ( ) 
  
    -- Perform difficultz calculatus when time is involved 
  
    if hour >= 0 and hour < 2 then 
        if hour == 1 then minute = minute + 60 end 
        local vol = 0.8 * getEasingValue ( ( 1 / 120 ) * minute, "InQuad" ) 
        setSoundVolume ( sound, vol ) 
    elseif hour >= 5 and hour < 7 then 
        if hour == 6 then minute = minute + 60 end 
        local vol = 0.8 * ( 1 - getEasingValue ( ( 1 / 120 ) * minute, "OutQuad" ) ) 
        setSoundVolume ( sound, vol ) 
    elseif hour >= 2 and hour < 5 then 
        setSoundVolume ( sound, 0.8 ) 
    else 
        setSoundVolume ( sound, 0 ) 
        return 
    end 
  
    --Calculate a reasonable speed 
    velocity = velocity + 1.5 
    speed = velocity / 3 
    setSoundSpeed( sound, speed ) 
end 
addEventHandler("onClientRender", root, adjustSoundSpeed) 
  
  

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...