Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Recommended Posts

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

Link to post
Share on other sites
  • 4 weeks later...

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 smile.png

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 ?

  • Upvote 1
Link to post
Share on other sites
  • 2 months later...

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 :)

  • Upvote 1
Link to post
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...