War Battle tutorial issue with the rockets

Hi, I’m a begginer and I’m going throught the “War Battles Tutorial”. For the moment I can move the character, but I when I press the “fire key”, I don’t see any rocket. I tried few thing, but it didn’t worked. Here’s the whole program:

function init(self)
	msg.post(".", "acquire_input_focus")

	self.moving = false
	self.firing = false                     -- [1]

	self.input = vmath.vector3()
	self.dir = vmath.vector3(0, 1, 0)
	self.speed = 50
end

function final(self)                                -- [7]
	msg.post(".", "release_input_focus")            -- [8]
end

function update(self, dt)
	if self.moving then
		local pos = go.get_position()
		pos = pos + self.dir * self.speed * dt
		go.set_position(pos)
	end

	if self.firing then
		factory.create("#rocketfactory")    -- [1]
	end

	self.input.x = 0
	self.input.y = 0

	self.moving = false
	self.firing = false                     -- [2]
end

function on_input(self, action_id, action)          -- [14]
	if action_id == hash("up") then
		self.input.y = 1                            -- [15]
	elseif action_id == hash("down") then
		self.input.y = -1
	elseif action_id == hash("left") then
		self.input.x = -1
	elseif action_id == hash("right") then
		self.input.x = 1
	elseif action_id == hash("fire") and action.pressed then
		self.firing = true
	end

	if vmath.length(self.input) > 0 then
		self.moving = true                          -- [16]
		self.dir = vmath.normalize(self.input)      -- [17]
	end
end

maybe you didn’t set in input_binding the “fire”

Hi!

In the output panel, does it print any errors or indicators of what could be wrong? The code seems correct, as far as I can see. :slight_smile: The variable self.firing seems to be set correctly to true when the action fire is triggered, so it would be interesting to see if the call factory.create("#rocketfactory") works?

The input_binding has the right name, but thanks for you help

So I add “print(self.firing)” in the update() fonction. When I press the key, it’s change to TRUE, and then it goes back to FALSE. I guess I have an issue with the " #rocketfactory", what do you think?

If you take a look at the #rocketfactory component, is it pointing to the correct gameobject for the rocket? How does the rocket gameobject look, does it have a sprite? Does the sprite have a “strange” position, like Z being set outside [-1…1]?

First, I’ve forgotten to add a sprite to the rocket, but it’s done now. (Still not working). When I click on Main>Player>Rocketfactory, the Prototype is the right component, but I don’t know how do you directly take a look at “#rocketfactory

1 Like

Yes, that’s what he meant, clicking on the rocketfactory and double checking that the prototype reference looks ok, that the sprite is there and and that the Z value is within the range [-1, 1]

1 Like

The rocket’s sprite has 1 for its Z value. So I still don’t know what I’ve done wrong. :confused:

Does it work if you set it to zero?

YES! It’s worked. Thank you a lot. Just one more thing, can you explain why does it work with the new value?

1 Like

The default renderer draws anything between -1 and 1 (fractional values allowed). The z-value of a sprite component is the sum of z-values of the game object z-value and sprite. The game object itself was likely spawned at depth 1 which means that the component was at 1+1=2 z.

2 Likes

I see now that the call to factory.create() didn’t specify a position which means that the spawned rocket was given the same position as the game object that spawned it.

2 Likes