reepblue Posted September 13, 2015 Share Posted September 13, 2015 Hi all, I'm pretty much almost done with the basic setup for my trip wires. The only problem I seem to have is that scaling the laser so it does not go beyond it's starting point and it's ending point which is always updating. My Code: function Script:Start() -- Ignore pickers from picking us! self.entity:SetPickMode(0) self.startpoint = self.entity:GetPosition(true) self.distance = Transform:Point(0,0,20,self.entity,nil) -- Sprites self.sprite={} -- The end sprite self.sprite[0] = Sprite:Create() local material = Material:Load("Materials/Effects/VecBall/vecball.mat") self.sprite[0]:SetMaterial(material) self.sprite[0]:SetSize(0.25,0.25) self.sprite[0]:Show() -- The beam self.sprite[1] = Sprite:Create() local material = Material:Load("Materials/Effects/laser1.mat") self.sprite[1]:SetMaterial(material) self.sprite[1]:SetViewMode(6)--Rotate around z axis --self.sprite[1]:SetSize(0.25,10) self.sprite[1]:Show() self.positionbetween=Pivot:Create() end function Script:UpdateWorld() self:UpdateLaser() end function Script: UpdatePhysics() end function Script:UpdateLaser() local pickInfo=PickInfo() local p0 = self.startpoint local p1 = self.distance self.endpoint = pickInfo.position if self.entity.world:Pick(p0,p1, pickInfo, 0, true, Collision.Prop) then self.sprite[0]:SetPosition(self.endpoint) --if(pickInfo.entity:GetClass() == Object.ModelClass) then if pickInfo.entity.script ~= nil then if type(pickInfo.entity.script.TakeDamage)=="function" then pickInfo.entity.script:TakeDamage(1) end end --end end self.positionbetween:SetPosition((p0+self.endpoint)/2) self.sprite[1]:SetPosition(self.positionbetween:GetPosition()) self.allignvector=p0-self.endpoint self.sprite[1]:AlignToVector(self.allignvector:Normalize(),2) self.sprite[1]:SetSize(0.25,self.endpoint.z/2) end function Script:PostRender(context) if DEBUG then local player = GameRules:GetPlayer() local p1 = player.script.camera:Project(self.startpoint); local p2 = player.script.camera:Project(self.endpoint); context:DrawLine(p1.x, p1.y, p2.x, p2.y); end end function Script:Release() if self.emitter~=nil then self.emitter[0]:Release() self.emitter=nil end end What I have now is OK for now, but I'd like to have this fixed eventually. When the model is pretty far from the starting point, it looks fine EDIT: Tried it with a longer trail, still messed up., but as the box gets closer to the source of the laser, the laser is drawn through the box. It's also axis based/dependent too, so that's not good ether! I need to get the distance from the midpoint and some how implement it to the laser's scale. I also having a hard time of it registering player collision. But I assume my main problem with that right now is that the player is a pivot. Also would like to mention that I'm using code from this topic. Any help would be appreciated. 1 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...
Jazz Posted September 14, 2015 Share Posted September 14, 2015 I played with it and came up with this. Set up for the workshop turret with standard materials for the laser. Can now select constant rotate CW and rotate CCW and set turret at any angle. Laser center pivot removed(not needed). Create a child pivot for the muzzle called muzzle. Fun stuff! edit: Josh' effects from the following post added and tweaked a bit. No default spark material but default tracer works pretty well. Adjusted flare's position so it comes out a bit. laser.lua 6 Quote --- Scott Using Windows 7 Ultimate 64 bit/Core I7-2700K @ 4312mhz/24G RAM/Nvidia GTX 1060 Link to comment Share on other sites More sharing options...
reepblue Posted September 15, 2015 Author Share Posted September 15, 2015 Works like a charm! Thank you a whole bunch, and I'm sure your work will help others! 1 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...
Josh Posted September 15, 2015 Share Posted September 15, 2015 Added smoke cool particle effects at the terminus: --place a child pivot for the muzzle called muzzle Script.laserdistance = 200 --float "Laser Distance" Script.startpoint = Vec3() Script.distance = nil Script.endpoint = Vec3() Script.box = nil Script.beamwidth = .01 --float "Beam Width" Script.rotateCW = false --bool "Rotate Clockwise" Script.rotateCCW = false --bool "Rotate CClockwise" Script.muzzle = nil Script.muzzlepos = nil function Script:Start() -- Ignore pickers from picking us! self.entity:SetPickMode(0) self.box = Model:Box(.1,.1,.1) self.box:SetPosition(0,-100,0) -- Sprites self.sprite={} -- The end sprite self.sprite[0] = Sprite:Create() local material = Material:Load("Materials/Effects/lensflare.mat") self.sprite[0]:SetColor(0,1,1) self.sprite[0]:SetMaterial(material) self.sprite[0]:SetSize(0.25,0.25) self.sprite[0]:Show() -- The beam self.sprite[1] = Sprite:Create() local material = Material:Load("Materials/Common/NavMesh.mat") self.sprite[1]:SetMaterial(material) self.sprite[1]:SetViewMode(6)--Rotate around z axis self.sprite[1]:Show() self.positionbetween=Pivot:Create() --Let's add some effects! self.emitter = {} self.emitter.smoke = Emitter:Create(5) self.emitter.smoke:SetDuration(500) self.emitter.smoke:SetEmissionVolume(0,0,0) self.emitter.smoke:SetVelocity(0,0,1,0) self.emitter.smoke:SetVelocity(0.25,0.25,0.25,1) self.emitter.smoke:ClearAlphaControlPoints() self.emitter.smoke:AddAlphaControlPoint(0.5,0.3) self.emitter.smoke:AddAlphaControlPoint(1.0,0.0) self.emitter.smoke:SetParticleColor(0.25,0.25,0.25,1,0) self.emitter.smoke:SetParticleColor(0.25,0.25,0.25,1,1) self.emitter.smoke:ClearScaleControlPoints() self.emitter.smoke:AddScaleControlPoint(0,1) self.emitter.smoke:AddScaleControlPoint(1,1) self.emitter.smoke:SetMaxScale(4) self.emitter.sparks = Emitter:Create(4,self.emitter.smoke) self.emitter.sparks:SetMaterial("Materials/Effects/spark.mat") self.emitter.sparks:SetDuration(200) self.emitter.sparks:SetEmissionVolume(0,0,0) self.emitter.sparks:SetVelocity(0,0,2,0) self.emitter.sparks:SetVelocity(4,4,4,1) self.emitter.sparks:SetAcceleration(0,-10,0) self.emitter.sparks:SetRotationByVelocityMode(true) self.emitter.sparks:ClearAlphaControlPoints() self.emitter.sparks:AddAlphaControlPoint(0.5,1.0) self.emitter.sparks:AddAlphaControlPoint(1.0,0.0) self.emitter.sparks:SetParticleColor(0,1,1,1,0) self.emitter.sparks:SetParticleColor(0,1,1,1,1) self.emitter.sparks:ClearScaleControlPoints() self.emitter.sparks:AddScaleControlPoint(0,1) self.emitter.sparks:AddScaleControlPoint(1,1) self.emitter.sparks:SetMaxScale(0.5) self.emitter.sparks:SetReleaseQuantity(1) self.emitter.sparks:SetRotation(0,0,0) self.muzzle = self.entity:FindChild("muzzle") if self.muzzle == nil then System:Debug("muzzle not found") end self.muzzlepos = self.muzzle:GetPosition(true) end function Script:UpdateWorld() self:UpdateLaser() end function Script: UpdatePhysics() end function Script:UpdateLaser() local rotation0 self.startpoint = self.muzzle:GetPosition(true) self.distance = Transform:Point(0, 0, self.laserdistance, self.entity, nil) --in case laser hits nothing create a box to hit or it throws beam off center --not needed if in enclosed space self.distancenohit = Transform:Point(0, 0, self.laserdistance - .3, self.entity, nil) local pickInfo=PickInfo() local p0 = self.startpoint local p1 = self.distance local picked = self.entity.world:Pick(p0,p1, pickInfo, 0, true, Collision.Prop) if picked then self.endpoint = pickInfo.position local speed = 0.1 self.emitter.smoke:AlignToVector(pickInfo.normal.x,pickInfo.normal.y,pickInfo.normal.z,2) self.emitter.sparks:AlignToVector(pickInfo.normal.x,pickInfo.normal.y,pickInfo.normal.z,2) --if(pickInfo.entity:GetClass() == Object.ModelClass) then if pickInfo.entity.script ~= nil then if type(pickInfo.entity.script.TakeDamage)=="function" then -- pickInfo.entity.script:TakeDamage(1) end end --following else is not needed if in enclosed space else self.box:SetPosition(self.distancenohit,true) picked = self.entity.world:Pick(p0,self.distancenohit, pickInfo, 0, true, Collision.Prop) self.endpoint = self.distance end self.allignvector = self.startpoint - self.endpoint self.sprite[0]:SetPosition(self.endpoint,true) self.sprite[0]:AlignToVector(self.allignvector:Normalize(),2) self.sprite[1]:SetPosition((p0+self.endpoint)/2) self.sprite[1]:AlignToVector(self.allignvector:Normalize(),2) self.emitter.smoke:SetPosition(self.sprite[0]:GetPosition(true)) --find distance between 2 points local distanceX = math.pow(self.startpoint.x - self.endpoint.x, 2) local distanceY = math.pow(self.startpoint.y - self.endpoint.y, 2) local distanceZ = math.pow(self.startpoint.z - self.endpoint.z, 2) local total_distance = math.sqrt(distanceX + distanceY + distanceZ) if self.rotateCW then rotation = self.entity:GetRotation(true) rotation.y = rotation.y + Time:GetSpeed()*0.1 self.entity:SetRotation(rotation) elseif self.rotateCCW then rotation = self.entity:GetRotation(true) rotation.y = rotation.y - Time:GetSpeed()*0.1 self.entity:SetRotation(rotation) end self.sprite[1]:SetSize(self.beamwidth, math.abs(total_distance)) end 8 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Slastraf Posted February 12, 2019 Share Posted February 12, 2019 function Script:CastRay(other) local sprite = Sprite:Create() sprite:SetViewMode(6) local p0 = self.entity:GetPosition(true) local p1 = other:GetPosition(true) self.allignvector = p0 - p1 sprite:SetPosition((p0+p1)/2) sprite:AlignToVector(self.allignvector:Normalize(),2) -- Find distance between the two points. local total_distance = p0:DistanceToPoint(p1) -- Modify the size of the beam based on the 2 points. -- Argument #1 is beamwidth, default .25 sprite:SetSize(.25, total_distance) end Here the script again , inside a function where other (in the parameter) is an entity. Also used LE Math functions to make it run much faster. Quote 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.