im currently making a code largely based off the war battles defold tutorial, however i only need the rockets to move upwards and they currently only do that when moving right, i need them to do it whatever direction my UFO is going in. i do not yet understand defold that well so please bare with.
im using:
local angle = math.atan2(self.dir.y, self.dir.x)
local rot = vmath.quat_rotation_z(angle)
local props = { dir = self.dir }
factory.create("#bulletfactory",nil, rot, props)
I’m not familiar with the tutorial in question but it seems clear what the issue is regardless.
When creating the bullet, you pass in a table of properties (“props”). This contains a direction (“dir”). Instead of passing in self.dir there (which is presumably the direction of your player object) you should just always pass in the direction you want, in this case up. That would be vmath.vector3(0,1,0).
The tutorial also has you calculate an angle and a rotation for the bullet. Again, you won’t need to do this since the rotation should always be the same. You’ll need to figure out the correct rotation (print() is your friend!) and pass that in instead of calculating a rotation based on the player direction.
i have the rotation down now thank you so much for explaining it that was very helpful, but it doesnt move and im not sure how to do that just yet please could you help? sorry!!
So you are passing a direction to the position parameter, and a speed to the rotation parameter.
Try and understand what you’ve done differently!
All of this said, you don’t actually need to pass a direction to the bullet, because you know it should always be the same. So, in your bullet script, just say:
go.property("dir", vmath.vector3(0,1,0))
And forget about passing a direction from the UFO.
thank you, sorry ive been messing around with the code a bit. i tried the original format i had from the tutorial again to work backwards but now it just spawns on the top right.
function update(self, dt)
if self.moving then
local pos = go.get_position()
pos = pos + self.dir * self.speed * dt -- direction and speed
go.set_position(pos)
end
if self.firing then
local angle = math.atan2(self.dir.y, self.dir.x) -- [1]
local rot = vmath.quat_rotation_z(angle) -- [2]
local props = { dir = self.dir } -- [3]
factory.create("#bulletfactory", nil, rot, props) -- rocket needs movement direction and game object direction for rot
end
please could you point out why it is doing this? sorry i am so bad, practice makes perfect i guess.