I'm having a problem with firing bullets in a game

Hello,

I’m a newbie in Defold but I really like the engine. I’ve been programming in other languages like Javascript and PHP. And now started playing with games.

So I am having problems with a top down shooter game I’m doing. The bullets the character fires stays at the place it was created. No animation is happening.

I’m using the Oneroom project as my follow example. So I’m using the same example. Don’t know where I’m going wrong.

When I fire a bullet it just stays there like this:

So I know the factory creating of the bullet.go is working, but what about the animation. So here we go.

  1. First when someone clicks the mouse it calls this function
	local function spawn_bullet(angle, properties)
	factory.create("#bulletfactory", go.get_world_position() + vmath.rotate(angle, BULLET_OFFSET), angle, properties)
end
  1. The bullet.go is inside the factory and has this on it.
components {
  id: "script"
  component: "/main/bullet.script"
  position {
    x: 0.0
    y: 0.0
    z: 0.0
  }
  rotation {
    x: 0.0
    y: 0.0
    z: 0.0
    w: 1.0
  }
}
embedded_components {
  id: "sprite"
  type: "sprite"
  data: "tile_set: \"/main/laserHero.atlas\"\n"
  "default_animation: \"fxShot\"\n"
  "material: \"/builtins/materials/sprite.material\"\n"
  "blend_mode: BLEND_MODE_ALPHA\n"
  ""
  position {
    x: 0.0
    y: 0.0
    z: 0.0
  }
  rotation {
    x: 0.0
    y: 0.0
    z: 0.0
    w: 1.0
  }
}
  1. And the bullet.script inside the bullet.go has this code
go.property("speed", 200)
go.property("type", hash("fxShot"))
go.property("enemy_bullet", false)
go.property("damage", 1)
go.property("distance", 500)
go.property("destroy_walls", false)
go.property("explosion", hash("explosion_small"))
go.property("particles", hash(""))

local COLLISION_RESPONSE = hash("collision_response")

function init(self)
msg.post("#sprite", "play_animation", { id = self.type })
    self.initial_position = go.get_position()
end

function final(self)
    
end

function update(self, dt)

    local pos = go.get_position()
    print("pos1: " .. pos)

	local d = vmath.rotate(go.get_rotation(), vmath.vector3(0, 1, 0))

    print("dt: " .. dt)

	pos = pos + d * self.speed * dt
	go.set_position(pos)
	print("pos2: " .. pos)

	--local distance_traveled = vmath.length(pos - self.initial_position)
end

function on_message(self, message_id, message, sender)
    if message_id == COLLISION_RESPONSE then

    end
end

function on_input(self, action_id, action)
    -- Add input-handling code here
    -- Remove this function if not needed
end

I am guessing the part where the bullet is supposed to move is this one:

pos = pos + d * self.speed * dt
go.set_position(pos)

But I don’t know what to change or where I went wrong.

If you want to know what happen when I click

2 Likes

You have a bunch of print() in the update function. What do you get from them in the console?

1 Like

Is the script attached to the bullet? Add some print() calls to the update() function and see what happens.

1 Like

That was just my way of debugging to see why the position is not changing.

Actually I get the position like this.

I’m getting the position, this is what is showing up:
DEBUG:SCRIPT: pos2: [455.704285, 385.823608, 0.000000]
DEBUG:SCRIPT: pos1: [468.517334, 381.935516, 0.000000]
DEBUG:SCRIPT: dt: 0.016666667535901

And it’s changing.

Funny now it kind of worked.

1 Like

That was one of the first mistakes I did britzl. hahaha. But I fixed that already. The animation problem was happening even after I added the script to the bullet.

And thanks for the Oneroom project, it does helps a newbie like me.

Now it seems to be working, don’t know what was happening before at all. So hope it keeps on working.

1 Like

Something kind of weird is happening now.

When I first click Build and Run it doesn’t work. But the second time I click Build and Run it it finally fires. Does it take some time to load?

No. Are you sure you didn’t save between the runs?