I wanted my game to have a shootout while standing on top of a train, but I can't get the train to move while keeping the player on top of it. So, I'm changing my way of thinking and now I want to leave the train standing still, and make everything else move past it (with the illusion of movement).
I wanted a script to make non-NPC entities [train tracks] move in a straight line, then go back to where they started in order to keep moving down that straight line over and over again.
I tried using the boat script, but after trying it about 50 different ways, I concluded that the solution wasn't the boat script.
I moved on to see if it could be done with the trap_wall script (by smallg) with a delay script (by 3com) thrown in with it. I had to work at altering the numbers for hours.
I can't figure out how to make the entities go far (more then 2,000 to 5,000) without making it stop working. When I make the trigger_range and the move_range numbers too far apart, it goes a short distance and stops, or it goes hay-wire.
It's a bit sloppy and I don't exactly know how to clean it up to be a little better, but here it is:
local move_speed = {}
local trigger_range = {}
local damage_range = {}
local move_range = {}
local moved = {}
local damage = {}
local repeating = {}
local startx = {}
local starty = {}
local startz = {}
--set entity to immobile
--sound at slot 0 will loop for a sliding wall etc effect
function trap_wall5_init(e)
--if trap will reset after player moves away or dies etc
repeating[e] = 1
--when to start moving the wall
trigger_range[e] = 2200 -- was 260
--max range at which damage can be applied (also requires walls to be closed)
damage_range[e] = 80
--wall will stop getting closer after it's moved this far (damage will stop too)
move_range[e] = 2000 -- originally 230
--how fast the wall will "slide"
move_speed[e] = 350
--damage per 0.25s
damage[e] = 10
end
function trap_wall5_main(e)
if repeating[e] == 1 and (startx[e] == nil or starty[e] == nil or startz[e] == nil) then
CollisionOff(e)
startx[e] = g_Entity[e]['x']
starty[e] = g_Entity[e]['y']
startz[e] = g_Entity[e]['z']
else
if GetPlayerFlatDistance(e) < trigger_range[e] then
if moved[e] == nil then
moved[e] = 0
end
LoopSound(e,0)
if moved[e] < move_range[e] then
CollisionOff(e)
MoveForward(e,move_speed[e])
moved[e] = moved[e] + 1
--CollisionOn(e)
if moved[e] > move_range[e] * 0.9 then
if GetPlayerFlatDistance(e) < damage_range[e] then
if GetTimer(e) > 250 then -- originally this was 250
StartTimer(e)
HurtPlayer(e,damage[e])
end
end
end
else
StopSound(e,0)
end
else
--CollisionOn(e)
StopSound(e,0)
if repeating[e] == 1 then
moved[e] = 0
CollisionOff(e)
SetPosition(e,startx[e],starty[e],startz[e])
--CollisionOn(e)
end
end
end
end
function GetPlayerFlatDistance(e)
tPlayerDX = (g_Entity[e]['x'] - g_PlayerPosX)
tPlayerDZ = (g_Entity[e]['z'] - g_PlayerPosZ)
return math.sqrt(math.abs(tPlayerDX*tPlayerDX)+math.abs(tPlayerDZ*tPlayerDZ));
end
function delay_script_main(e)
GetEntityPlayerVisibility(e)
PlayerDist = GetPlayerDistance(e)
if PlayerDist < 300 and g_Entity[e]['plrvisible'] == 1 then
--timer1[e] = os.time()
--Prompt("timer1[e]: " ..timer1[e])
if os.time() > timer1[e] + 1 then
--PromptDuration("os.time1: " ..os.time() .." - timer1[e]: " ..timer1[e],3000)
ActivateIfUsed(e)
SwitchScript(e,"door_auto")
timer1[e] = os.time()
end
end
end