Jump to content

How to make a Turret shoot from Fpsgun script?


Slastraf
 Share

Recommended Posts

I am currently getting problems with the Fpsgun script.

I made a prefab and attached it to an turret in the map via path.

http://pastebin.com/LapGp5nw

Heres the modified fpsgun script.

In detail the problem is that the muzzle (shooting direction) should always be in front direction of the turret.

The second problem is that the script doesnt know the fpsplayer anymore, so there is no camera to align the effects to.

There are going to be multiple turrets in the game, so there should be a global camera, but I dont know how to set it up .

I have tried to fix it but it doesnt work really well.

I would apprechiate any help on this.

Link to comment
Share on other sites

just a simple example of a turret tracking and shooting

window = Window:Create("Example",0,0,800,600)

context = Context:Create(window)

world = World:Create()

camera = Camera:Create()

camera:SetPosition(0,7,0)

camera:SetRotation(90,0,0)

light = DirectionalLight:Create()

light:SetRotation(45,45,0)

 

ground = Model:Box(10,1,10)

ground:SetPosition(0,0,-0.5)

ground:SetColor(0,1,0,1)

 

base = Model:Cylinder(16)

base:SetColor(0,0,1,1)

base:SetPosition(4,1,4)

base:SetPickMode(0)

turret = Model:Box(base)

turret:SetScale(0.1,0.1,1)

turret:SetColor(1,0,0,1)

turret:SetPosition(0,0.5,0.5,false)

firespot = Pivot:Create(turret)

firespot:SetPosition(0,0,1,false)

bullet = Model:Box()

bullet:SetScale(0.1,0.1,0.1)

bullet:SetPickMode(0)

bullet:Hide()

 

target = Model:Sphere(16)

target:SetColor(1,0.5,0,1)

target:SetPickMode(0)

 

pickinfo = PickInfo()

 

time = Time:Millisecs()

 

function PerfectAngle(pos1,pos2)

local dx, dz

dx = pos2.x - pos1.x

dz = pos2.z - pos1.z

return Math:ATan2(dx,dz)

end

 

while window:KeyDown(Key.Escape)==false do

if window:Closed() then break end

 

mousepos = window:GetMousePosition()

if camera:Pick(mousepos.x,mousepos.y,pickinfo,0,true,0) then

target:SetPosition(pickinfo.position + Vec3(0,1,0))

end

 

perfectangle = PerfectAngle(base:GetPosition(true),target:GetPosition(true))

currentangle = base:GetRotation(true).y

angle=Math:IncAngle(perfectangle,currentangle,5/Time:GetSpeed())

base:SetRotation(0,angle,0,true)

 

if Time:Millisecs() > time + 500 then

bullets = bullet:Instance()

bullets:SetPosition(firespot:GetPosition(true))

bullets:Show()

bullets:SetMass(1)

force = Vec3(2000*Math:Sin(angle),0,2000*Math:Cos(angle))

bullets:AddPointForce(force,firespot:GetPosition(true),true)

time = Time:Millisecs()

end

 

Time:Update()

world:Update()

world:Render()

context:Sync(true)

 

end

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

When one does not give enough information or assets for others to use, you get simple examples that may or may not resolve their issue based on what assumptions we have to make. Anything that is shown in a simple main.lua script can be worked into an entity script. Is there a particular reason why the bullet/shooting would be attached to a pivot? Why would you not have the bullet/shooting code as part of the turret tracking script? Are you wanting a raycast or an actual physics bullet?

 

If a physics bullet, there are a couple of ways to make the bullet move in the direction of the target. Above I show how to calculate the forces to control the angle that the bullet will go with the assumption that the turret just rotates solely around the Y-axis. You can also go about it another way: set the position/rotation of the bullet on the local axis of the turret, and then just apply a force in the local +Z-axis direction. See here for an old LE2 example of that: http://www.leadwerks.com/werkspace/topic/2542-bullet-help-wanted/#entry23432

 

If a raycast bullet, then I suggest just using a simple World:Pick() between the end of the turret and the target.

  • Upvote 1

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
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.

 Share

×
×
  • Create New...