reepblue Posted December 6, 2016 Share Posted December 6, 2016 A beam script that'll stretch a sprite from one point to another. Useful for lasers and cables. You can enable or disable auto updating if it isn't necessary. --[[ This script will create a simple sprite that looks like a beam or a cable. The beam is does not do any raycasting along and gets updated via the physics step. You can apply a script to the beam to change it's appearance such as flickering or pulsing. ]]-- Script.fxscriptfile=""--path "Beam Script" "Script (*.lua):lua|Scripts/Objects/Effects" Script.beamcolor=Vec4(1,1,1,1) --color "Beam Color" Script.beambrightness=1.0--float "Beam Brightness" Script.materialfile=""--path "Material" "Material (*.mat):mat|Materials" Script.beamwidth = .25 --float "Beam Width" Script.startpoint = nil --entity "Start Position" Script.endpoint = nil --entity "End Position" Script.beamhidden = false --bool "Hidden" Script.autoupdate = true --bool "Auto-Update" function Script:Start() -- Ignore pickers from picking us! self.entity:SetPickMode(0) -- Beam Sprite self.sprite = Sprite:Create() local material = Material:Load(self.materialfile) if material then self.sprite:SetMaterial(material) material:Release() else System:Debug("Beam material is missing.") end self.sprite:SetViewMode(6)--Rotate around z axis if self.fxscriptfile ~= nil then self.sprite:SetScript(self.fxscriptfile) end self.sprite:SetColor(self.beamcolor) self.sprite:SetIntensity(self.beambrightness) if not self.beamhidden then self.sprite:Show() else self.sprite:Hide() end -- If the first feild is nil, assume the user want's it to be the entity that holds this script. if self.startpoint == nil then self.startpoint = self.entity end -- A end point is needed. If nil, report error. if self.endpoint == nil then System:Debug("End point is nil.") end -- Only draw the beam once of auto-update is false. if not self.autoupdate then self:UpdateBeam() end end function Script:UpdateBeam() -- Return if the beam is hidden. if self.sprite:Hidden() then return end local p0 = self:GetStartPoint() local p1 = self:GetEndPoint() self.allignvector = p0 - p1 self.sprite:SetPosition((p0+p1)/2) self.sprite:AlignToVector(self.allignvector:Normalize(),2) -- Find distance between the two points. local distanceX = math.pow(p0.x - p1.x, 2) local distanceY = math.pow(p0.y - p1.y, 2) local distanceZ = math.pow(p0.z - p1.z, 2) local total_distance = math.sqrt(distanceX + distanceY + distanceZ) -- Modify the size of the beam based on the 2 points. self.sprite:SetSize(self.beamwidth, math.abs(total_distance)) end function Script:UpdatePhysics() if self.autoupdate then self:UpdateBeam() end end function Script:GetStartPoint() return self.startpoint:GetPosition(true) end function Script:GetEndPoint() return self.endpoint:GetPosition(true) end function Script:TurnOn()--in if self:IsOn() then return end self.sprite:Show() end function Script:TurnOff()--in if not self:IsOn() then return end self.sprite:Hide() end function Script:Toggle()--in if self:IsOn() then self:TurnOff() else self:TurnOn() end end function Script:IsOn() return not self.sprite:Hidden() end function Script:Release() self.sprite:Release() end If you're looking for a laser beam in which the end position is the end of a pick test, you can check out this post for details on how to do it. This script just has the basic functionality. You can reuse the script for more complex effects by ether cloning the script, or setting the script to an entity and then modifying it's values like. -- Make Pivot local p = Pivot:Create() -- Set Script to Pivot p:SetScript("scripts/objects/effects/beam.lua") -- Modify some values p.script.beamcolor=Vec4(1,0,0,1) p.script.autoupdate = false .... .... 5 Quote Cyclone - Ultra Game System - Component Preprocessor - Tex2TGA - Darkness Awaits Template (Leadwerks) If you like my work, consider supporting me on Patreon! Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.