Braqoon 19 Posted October 27, 2015 Share Posted October 27, 2015 Hi, So I can't find a good example or tutorial on how to create a bullet projectile that will fire in given direction and vanish if get off screen or hit an obstacle. On the forum subject of bullets is mostly covered in FPS type game where you would use a raycasting with no visible bullet. Only post that relates to bullet I'm after is http://www.leadwerks.com/werkspace/topic/11941-bullet-hell/page__hl__bullet but to be honest there is no LUA code to start with and there is no answer actually how to go about it. Did somebody tried to do it ? Please correct me if I'm wrong but it looks like Sprite object will be the most suitable for this. Let's assume for this exercise that I got a 'turret' that fires in one direction if I press a button. On that action a sprite is being created from within a turret script (texture, initial position, etc) and is being pushed with eg. SetVelocity. (other ? please correct) 1. Can Sprite be created from within a turret script and have own LUA script attached which will describe potential interaction with the World like collision, Velocity etc) 2. Will that bullet become own entity in the World ? 3. Can it be destroyed/released/nullified from the World ? If yes, how? 4. Is is possible to determine is Sprite visible by the players camera ? (eg. Top down static view on a playing field I'm am still fairly new to Leadwerks but seems that games with shooting mechanics where Raycasting is not a good option as it requires a projectile to be visible and hit not being instant, are not covered too much. Thanks Quote Link to post Share on other sites
Josh 10,030 Posted October 27, 2015 Share Posted October 27, 2015 If you look in the FPSWeapon script it creates a bullet and uses a sprite for a tracer effect. That should serve as a basis for what you are trying to do. Let us know if you have any questions. Quote Link to post Share on other sites
Braqoon 19 Posted October 27, 2015 Author Share Posted October 27, 2015 OK, thanks. Will look into that. Quote Link to post Share on other sites
Sakru 57 Posted October 27, 2015 Share Posted October 27, 2015 Yeah I am too interested in how to do it. Quote Link to post Share on other sites
Braqoon 19 Posted November 21, 2015 Author Share Posted November 21, 2015 Hi, so I had some time to tinker and I think I got decent but basic bullet. Script that Josh suggested was useful but one thing I'm missing. I cannot make my bullet hit an object/enemy. My function to create a bullet is : function Script:GetBullet(position) local bullet = Sprite:Create() bullet:SetPosition(position) bullet:SetViewMode(2) bullet:SetMass(1) bullet:SetGravityMode(false) bullet:SetCollisionType(Collision.Prop) bullet:SetCollisionType(Collision.Projectile) bullet.origin = Vec3(position.x,position.y,position.z) local mat = Material:Load("Materials/Icons/PointLight.mat") if mat then bullet:SetMaterial(mat) mat:Release() end return bullet end function Script:UpdateWorld() local bullet,n,dist local travel local bspeed=22--float local window = Window:GetCurrent() for n,bullet in ipairs(self.bullets) do dist = (bullet.position-bullet.origin):Length() if dist>self.bulletrange then table.remove(self.bullets,n) bullet:Release() bullet=nil end if bullet ~=nill then travel = bspeed/60.0*Time:GetSpeed() bullet.position = bullet.position+Vec3(0,0,travel) bullet:SetPosition(bullet.position) end end if window:KeyDown(Key.E) then local currenttime=Time:GetCurrent() if self.lastfiretime==nil then self.lastfiretime=0 end if currenttime-self.lastfiretime>self.refirerate then self.lastfiretime = currenttime local pos = self.entity:GetPosition(true) table.insert(self.bullets,self:GetBullet(pos)) end end self:UpdateCamera() end Yeah, I'm shooting light bulbs at the moment Now, I assumed that sprite needs to have properties of a normal object so it can interact with environment but that does not work. Sprite goes right trough the object and nothing happens. My "enemy" script is very simple for now: function Script:Start() self.startposition = self.entity:GetPosition() -- Enemy Vars self.entity:SetMass(1) self.entity:SetGravityMode(false) self.entity:SetFriction(0,0) self.entity:SetCollisionType(Collision.Prop) end function Script:Collision(entity) self.entity:Hide() end But if I collide this enemy with my player controller which for now is just a primitive box, all works as expected, collision works. What I need to do for sprite to trigger this collision ? 1 Quote Link to post Share on other sites
Braqoon 19 Posted November 23, 2015 Author Share Posted November 23, 2015 Anyone ? @josh Quote Link to post Share on other sites
xtreampb 29 Posted January 29, 2016 Share Posted January 29, 2016 I don't think sprites has collision. maybe try to make a primitive small box and assign it mass and create it just like you do your sprite. Quote Link to post Share on other sites
Braqoon 19 Posted February 4, 2016 Author Share Posted February 4, 2016 I have make it work with self.entity.world:Pick(), re-sued from bullet.lua function Script:UpdateWorld() local bullet,n,dist local pickinfo=PickInfo() local travel local bspeed=25 local window = Window:GetCurrent() for n,bullet in ipairs(self.bullets) do travel = bspeed/60.0*Time:GetSpeed() if self.entity.world:Pick(bullet.position,bullet.position+travel,pickinfo,0,true,Collision.Projectile) then local enemy = self:FindScriptedParent(pickinfo.entity,"Hurt") table.remove(self.bullets,n) bullet:Release() bullet=nil if enemy~=nil then if enemy.script.health>0 then enemy.script:Hurt(self.bulletdamage,self.player) end end else if bullet ~=nill then dist = (bullet.position-bullet.origin):Length() travel = bspeed/60.0*Time:GetSpeed() bullet.position = bullet.position+Vec3(0,0,travel) bullet:SetPosition(bullet.position) if dist>self.bulletrange then table.remove(self.bullets,n) bullet:Release() bullet=nil end end end end if window:KeyDown(Key.E) then local currenttime=Time:GetCurrent() if self.lastfiretime==nil then self.lastfiretime=0 end if currenttime-self.lastfiretime>self.refirerate then self.lastfiretime = currenttime local pos = self.entity:GetPosition(true) table.insert(self.bullets,self:GetBullet(pos)) end end To be honest I'm not 100% how this works but it does, this Pick function 1 Quote Link to post Share on other sites
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.