Search the Community
Showing results for tags 'shooting'.
-
I would like to shoot a projectile towards a point in space (in a 2D game). I have something almost working but I can't get my head round how to fix it. I would like to solve the following: 1) How to shoot in a certain direction not just where the mouse is (I could only get it working right with this mouse pointer method but in reality I just want to shoot something in a direction (and maybe not use mouse). 2) The projectiles shoot out sort of 90 degrees to where the mouse pointer is but I want them to shoot at where mouse pointer is. I seem to get automatically very confused when thinking about angles. 3) At certain positions (90 degrees?) the projectiles sort of flip to the other side when I really just want it to track smoothly all the way round. Here is the code. Can anyone help? Maybe there is just a better way to do it? function App:Start() --Create a window self.window = Window:Create() self.context = Context:Create(self.window) self.world = World:Create() local camera = Camera:Create() camera:SetRotation(35,0,0) camera:Move(0,0,-6) local light = DirectionalLight:Create() light:SetRotation(35,35,0) --Create a model self.model = Model:Box() return true end function App:Loop() if self.window:Closed() or self.window:KeyHit(Key.Escape) then return false end --Get the vector between the mouse position and the center of the screen local v = self.window:GetMousePosition() local dx = v.x - self.context:GetWidth()/2.0 local dy = self.context:GetHeight()/2.0 - v.y v = Vec3(dx,dy,0):Normalize() --Align the model to the vector self.model:AlignToVector(v,2) -- Create projectile bullet = Model:Sphere() bullet:SetPosition(5,0,0) bullet:SetMass(1.0) -- Align to mouse pointer vector bullet:AlignToVector(v, 2) -- Add velocity bullet:SetVelocity(Vec3(0,10,0), false ) Time:Update() self.world:Update() self.world:Render() self.context:Sync() return true end Thanks, Michael
-
Heya, everyone. I was wondering if someone can help me fix this problem i'm having. I am trying to create a turret that targets someone when they come in range of it and shoots them. I got all that working fine, but this turret can shoot through walls. I was wondering if someone had any idea what I need to change on the walls, or the code to enable a Line of Sight factor. This is the script atm i'm using. Script.aimingLocation = "" --entity "Aiming Location" Script.rotSpeed = .1 --float "Rotation Speed" Script.fireRate = .1 --float "Fire rate" Script.fireRateTimer = 0 Script.muzzleFlashLight = "" --entity "Muzzle flash light" Script.muzzleTime = 0.1 --float "MuzzleTimer" Script.muzzleTimer = 0 Script.gunFireSoundPath = "" --path "Fire Sound" Wav file (*wav):wav|Sound" Script.gunFireSoundFile = nil Script.fireRange = 15 --float "Fire range" Script.damage = 5 --float "Damage per bullet" function Script:Start() self.muzzleFlashLight:Hide() if self.gunFireSoundPath ~= "" then self.gunFireSoundFile = Sound:Load(self.gunFireSoundPath) end end function Script:UpdateWorld() --fire rate self.fireRateTimer = self.fireRateTimer + (Time:GetSpeed()/100) self.muzzleTimer = self.muzzleTimer + (Time:GetSpeed()/100) --get positions local turretPos = self.entity:GetPosition() local playerPos = self.aimingLocation:GetPosition(true) --is target in range? if(turretPos:DistanceToPoint(playerPos) <= self.fireRange) then --rotate turret toward target self.entity:Point(self.aimingLocation, 2, Time:GetSpeed() * self.rotSpeed) --allow firing if(self.fireRateTimer > self.fireRate) then local pickInfo = PickInfo() local point2 = Transform:Point(0,0,self.fireRange, self.entity, nil) if (self.entity.world:Pick(turretPos, point2 ,pickInfo, 0, true, Collision.Prop)) then self.aimingLocation:GetParent().script:Hurt(self.damage) end self.muzzleFlashLight:Show() self.muzzleTimer = 0 self.fireRateTimer = 0 self.gunFireSoundFile:Play() end end if(self.muzzleTimer > self.muzzleTime) then self.muzzleFlashLight:Hide() self.muzzleTimer = 0 end end