Talk:How To Build A Catapult

From Roblox Wiki

Jump to: navigation, search

Dear Gods of the Wiki: Please feel free to move this article anywhere once I am done writing it. I was not sure where to put it. --SonOfSevenless 12:32, 23 February 2007 (CST)

Dear Wiki Gods: The scripting template seems to come with a heavy intro that no one reading an advanced tutorial needs. Perhaps we should bust out a second scripting template for adv. tutorials?


I made the template in the hopes of decreasing writing time. It should probably be deleted, but I'm not an admin here.

---Gamer3D 12:54, 23 February 2007 (CST)


You seem to be making a TON of edits. Try to make just one, if possible. I was going to finish up this tutorial, but you have already done this. Good work.

---Gamer3D 13:38, 23 February 2007 (CST)

Testing

These work...

local powerblock = script.Parent.Parent.PowerBlock
local bodyforce = powerblock.BodyForce

local debounce = false

function onTouched(hit)
	local humanoid = hit.Parent:findFirstChild("Humanoid")
	if humanoid~=nil and debounce == false then
		debounce = true
		bodyforce.force = Vector3.new(0,1000000,0)
		wait(3)
		bodyforce.force = Vector3.new(0,0,0)
		wait(3)
		debounce = false
	end
end

script.Parent.Touched:connect(onTouched)

Catapult Trigger Script:

local powerblock = script.Parent.Parent.PowerBlock
local bodyforce = powerblock.BodyForce
local hotplate = script.Parent.Parent.HotPlate

local debounce = false

function onTouched(hit)
	local humanoid = hit.Parent:findFirstChild("Humanoid")
	if humanoid~=nil and debounce == false then
		debounce = true
		bodyforce.force = Vector3.new(0,1000000,0) --[[you may have to adjust this number, 
depending on the design of your catapult --]]
		wait(3)
		bodyforce.force = Vector3.new(0,0,0)
		wait(3)
		debounce = false
	end
end

function onHotPlateTouched(hit)
	if (hit.Name ~= "WarRock") then return end
	if (hit:findFirstChild("WarRockScript") ~= nil) then return end -- debounce
	hit.BrickColor = BrickColor.new(105)
	local code = script.Parent.WarRockScript:clone()
	code.Disabled = false
	code.Parent = hit
end

script.Parent.Touched:connect(onTouched)
hotplate.Touched:connect(onHotPlateTouched)

WarRock script:

-- attached to WarRock
print("WarRock script running")

r = game:service("RunService")

local warRock = script.Parent
local flightTime = 0               -- rock is only hot after we detect it has been flying for .5 seconds
local hotRock = false
local lastPos = nil

function onTouched(hit)
	-- We hit something, time to blow up
	if (hotRock == true) then
		blowUp()
	end
end

function getExplosionRadius(mass)
	if (mass > 50) then return 16 end
	if (mass > 40) then return 13 end
	if (mass > 5) then return 10 end
	return 6
end

function getExplosionPressure(mass)
	if (mass > 50) then return 2000000 end
	if (mass > 40) then return 1000000 end
	if (mass > 5) then return 500000 end
	return 200000
end


function blowUp()

	local sound = Instance.new("Sound")
		sound.SoundId = "rbxasset://sounds\\Rocket shot.wav"
		sound.Parent = warRock
		sound.Volume = 1
		sound:play()
	explosion = Instance.new("Explosion")

	-- As per my forum post, radius and pressure calculations are hacks
	local mass = warRock:getMass()
	-- in default war rocks, mass varies from 1 to 57

	explosion.BlastRadius = getExplosionRadius(mass)
	explosion.BlastPressure = getExplosionPressure(mass)
	explosion.Position = warRock.Position
	explosion.Parent = game.Workspace
	warRock.Parent = nil
end

function getDistance(a, b)
	local dx = a.x - b.x --[[change in x distance from a to b--]]
	local dy = a.y - b.y --[[change in y distance from a to b--]]
	local dz = a.z - b.z --[[change in z distance from a to b--]]
	return math.sqrt(dx * dx + dy * dy + dz * dz) --[[pythagorean theorem in 3D--]]
end


function checkFlying(dt)
	-- "flying" is movement of over 16 studs per second
	-- when this condition holds, increment the flightTime
	-- when flightTime > .5, WarRock goes hot!
	if (lastPos == nil) then 
		lastPos = warRock.Position 
		return
	end

	local velocity = getDistance(lastPos, warRock.Position) / dt --[[rate of change of distance over time, 
derivative --]]

	if (velocity > 16) then
		flightTime = flightTime + dt
	else
		flightTime = 0
	end


	if (flightTime > .5) then
		hotRock = true
		warRock.BrickColor = BrickColor.new(21)
	end

	lastPos = warRock.Position
end

warRock.Touched:connect(onTouched)

local lastTime = r.Stepped:wait()
local curTime


while true do
	curTime = r.Stepped:wait() -- should be 1/30th a second, but take no chances
	if (hotRock == false) then 
		checkFlying(curTime - lastTime) 
		lastTime = curTime
	else break end
end
MINDRAKER 16:53, 23 September 2008 (CDT)
Personal tools