Jump to content

Spawning a ramp in front of vehicle


Guest Gwaihir

Recommended Posts

Posted

Hi. I'd like to write script, which will create ramps in front of my car and after some seconds delete it, but It's not easy for me to create that script (I'm LUA newbie :P).

I'm not asking You to make this script for me, I'm asking You for solutions how to do it. I think I should check vehicle's position and rotation, but I'm not sure what to do later...

Sorry for my bad english. I hope You will help me :-)

Posted

local x, y, z = getElementPosition(player) 
local a = getPlayerRotation(player) 
  
x += (10 * math.sin(-a)) 
y += (10 * math.cos(-a)) 
  
createObject (modelid, x, y, z, 0, 0, a) 

^ you can create an object in front of the player with this code :)

Posted (edited)

Thank You very much :)I've got one more question...How can I make my script "sleep" for 50ms. ?

@down

OK, thx :)

Edited by Guest
Posted

x += (10 * math.sin(-a)) 
y += (10 * math.cos(-a)) 

=

SCRIPT ERROR: ...ramp.lua:5: '=' expected near '+' 

What does "x +=" mean? How can I fix this error?

Posted (edited)

function spawnRamp( thePlayer ) 
     local x, y, z = getElementPosition(player) 
     local a = getPlayerRotation(player) 
   
     x2 = (10 * math.sin(-a)) 
     y2 = (10 * math.cos(-a)) 
   
     local ramp = createObject (3593, x2, y2, z, 0, 0, a) 
     [color=#FF0000]setTimer ( removeRamp, 3000, 1, [b]ramp[/b] )[/color] 
   
  
 end 
   
  
[color=#FF0000]function removeRamp ( [b]rampObj[/b] )[/color] 
[color=#FF0000]     destroyElement(rampObj)[/color] 
[color=#FF0000] end[/color] 
 bindKey ( thePlayer, "c", "down", spawnRamp )  

You should have scripted it like above:

2 functions: 1 spawns the ramp, 1 destroys the ramp.

1 timer: inside the spawnfunction, that will trigger the destroy function when it expires.

EDIT: text in red is changed code, but noticed previous post got deleted, I also didn't verify whether your code really does what it is supposed to do, just the correct use of the 2 functions and the timer

Edited by Guest
Posted

unfortunatly '+=' wont work in lua.

i'd do something like that

function createRamp ( player, command ) 
    local x, y, z = getElementPosition ( player ) 
    local r = getPlayerRotation ( player ) 
    x = x - math.sin ( math.rad ( r ) ) * 5 
    y = y + math.cos ( math.rad ( r ) ) * 5 
    createObject ( 1655, x, y, z, 0, 0, r ) 
end 
addCommandHandler ( "ramp", createRamp ) 

Posted (edited)

Ok, and why so difficult? Just

function createRamp ( player, command ) 
    local x, y, z = getElementPosition ( player ) 
    local r = getPlayerRotation ( player ) 
    x = x + math.cos(r) * 10 
    y = y + math.sin(r) * 10 
    createObject ( 1655, x, y, z, 0, 0, -r ) 
end 
addCommandHandler ( "ramp", createRamp ) 

Damn. Something goes wrong in my script(

Edited by Guest
Posted

Thanks.

The last question: How can I remove ramp? :oops:

Posted

function createRamp ( player, command ) 
    local x, y, z = getElementPosition ( player ) 
    local r = getPlayerRotation ( player ) 
    x = x - math.sin ( math.rad ( r ) ) * 20 
    y = y + math.cos ( math.rad ( r ) ) * 20 
    createObject ( 1655, x, y, z, 0, 0, r ) 
    setTimer ( RemoveRamp, 10000, 1 ) 
end 
addCommandHandler ( "ramp", createRamp ) 
  
function RemoveRamp () 
        outputChatBox ( "Blablabla", thePlayer ) 
end 
  

It DOES work, when player is in vehicle :)

Posted

I have tried it. It works only when I'm not in a vehicle. When I'm in a vehicle, it spawns ramps with the static rotation, and it depends on the rotation what I have, when I entered the car. So, if I turn, and then spawn the ramp, I can't jump, because the rotation is wrong.

This script is fully working:

ffunction createRamp ( player, command ) 
    local x, y, z = getElementPosition ( player ) 
    local theVehicle = getPlayerOccupiedVehicle ( player ) 
    local a,b,r = getVehicleRotation ( theVehicle ) 
    x = x - math.sin ( math.rad(r) ) * 20 
    y = y + math.cos ( math.rad(r) ) * 20 
    ramp = createObject ( 1655, x, y, z, 0, 0, r ) 
    setTimer ( RemoveRamp, 8000, 1 ) 
end 
function RemoveRamp () 
        destroyElement(ramp) 
end 
addCommandHandler ( "r", createRamp ) 

Posted

Thank You, but there is little problem... The ramp spawns, but when You will spawn more than 1 ramps (type quickly /ramp twice), two ramps will apear, but only one will disapear. I wrote this:

function createRamp ( player, command ) 
    local x, y, z = getElementPosition ( player ) 
    local theVehicle = getPlayerOccupiedVehicle ( player ) 
    local a,b,r = getVehicleRotation ( theVehicle ) 
    if ( spawned == yes ) then 
        x = x - math.sin ( math.rad(r) ) * 20 
        y = y + math.cos ( math.rad(r) ) * 20 
        ramp = createObject ( 1655, x, y, z, 0, 0, r ) 
        spawned = yes 
        setTimer ( RemoveRamp, 8000, 1 ) 
        setTimer ( setClock, 8000, 1 ) 
    end 
    if (spawned == no) then 
        outputChatBox("Wait, please...", thePlayer) 
    end 
  
end 
  
function setClock () 
    spawned = no 
end 
  
  
  
function RemoveRamp () 
    destroyElement(ramp) 
end 
addCommandHandler ( "ramp", createRamp ) 

But it doesn't work :(

Posted

You can do it with much less difficulty:

You had:

  
ramp = createObject ( 1655, x, y, z, 0, 0, r ) 
... 
setTimer ( RemoveRamp, 8000, 1 ) 
  

just add the ramp as argument to that timer:

setTimer ( RemoveRamp, 8000, 1, ramp ) 

This way it will function as if ramp was an argument of the functioncall, usually written as:

RemoveRamp( ramp )

Posted

Thank You :) It works very good now! :D

function createRamp ( player, command ) 
    local x, y, z = getElementPosition ( player ) 
    local theVehicle = getPlayerOccupiedVehicle ( player ) 
    local a,b,r = getVehicleRotation ( theVehicle ) 
    x = x - math.sin ( math.rad(r) ) * 20 
    y = y + math.cos ( math.rad(r) ) * 20 
    ramp = createObject ( 1655, x, y, z, 0, 0, r ) 
    setTimer ( RemoveRamp, 8000, 1, ramp ) 
    end 
end 
  
function RemoveRamp (ramp) 
    destroyElement(ramp) 
end 
addCommandHandler ( "ramp", createRamp ) 

Posted

function createRamp ( source, key, keyState ) 
    local x, y, z = getElementPosition ( source ) 
    local theVehicle = getPlayerOccupiedVehicle ( source ) 
    local a,b,r = getVehicleRotation ( theVehicle ) 
    x = x - math.sin ( math.rad(r) ) * 20 
    y = y + math.cos ( math.rad(r) ) * 20 
    ramp = createObject ( 1655, x, y, z, 0, 0, r ) 
    setTimer ( RemoveRamp, 8000, 1, ramp ) 
end 
function RemoveRamp (ramp) 
    destroyElement(ramp) 
end 
  
  
function bindTheKeys ( source ) 
    bindKey ( source, "c", "down", createRamp ) 
    outputChatBox( "Press 'c' to spawn ramp (you must be in any vehicle)",thePlayer )    
end 
addCommandHandler ( "ramp", bindTheKeys ) 

I'm using this one and it works very good :)

You need to type /ramp to activate ramps. To spawn ramp in front of You, press C.

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...