Need rocket to only move upwards

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.

2 Likes

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!!

What do your scripts look like? The UFO and the bullet.


image
first is my player script so the UFO, the second is all i have so far for my bullet

It’s easiest to read code as text and not as screenshots. If you nest your code in three ` at the beginning and end it will be formatted nicely.

Like this

You are using factory.create() differently than from your first post. Here’s the API for factory.create():

It’s:

factory.create(url,[position],[rotation],[properties],[scale])

You’re doing:

factory.create("#bulletfactory", dir, speed)

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.

1 Like

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.

Yes, certainly practice makes perfect. I encourage you to continue experimenting.

I can’t immediately see why it would spawn in a strange location. Look at the API again:

The second parameter is position, so if you are passing in nil then it should spawn in the same position as the spawner, i.e. the UFO.

You don’t need to pass in props anymore because we don’t care about the UFOs direction when it comes to the bullets.

when i remove props, the rockets dont move at all though

Have you set self.dir in the bullet to be vmath.vector3(0,1,0)?

yes, it now moves but still spawning in the top right. no clue why, will keep trying