self.dir is really 0, because if I put some value. example (0, 1, 0) shots are fixed on the y axis
This is the projectile_slime.script
go.property("dir", vmath.vector3(0, 0, 0))
function init(self)
self.speed = 200 -- Velocidade do projetil
self.life = 1 -- Tempo de vida do objeto projetil em segundos
end
function update(self, dt)
local pos = go.get_position() -- Cria a variavel 'pos' e atribui a posição atual do objeto projetil
pos = pos + self.dir * self.speed * dt -- A variável 'pos' recebe a soma dos eixos (x, y, z) vezes a velocidade vezes o deltaT('deltaT ajusta o fps do aparelho mobile para melhor eficiência')
print('dir --- ',self.dir)
go.set_position(pos)
self.life = self.life - dt
if not self.collided then -- Se objeto projetil não colidir execute o código
local new_position = pos + self.dir * self.speed * dt
go.set_position(new_position) -- Atualiza posição do objeto projetil gerado
if self.life < 0 then -- Se o tempo do projetil for menor que 0, inicia a contagem ate tempo de vida do objeto após disparo
self.speed = 0
go.delete()
end
else
sprite.play_flipbook('#projetil_slime_enemy', hash('atirar'), go.delete()) -- Se colidir, delete o objeto projetil
end
end
function on_message(self, message_id, message, sender)
if message_id == hash('contact_point_response') then -- Se local de contato de 2 objetos
self.collided = true -- Colisão verdadeira
self.target = message.other_id -- Armazena id do objeto alvo
self.message_collision = true
end
end
This is the slime.script
go.property("health", 3)
go.property("speed", 50)
go.property("rate_of_fire", 10)
go.property("min_fire_distance", 0)
go.property("max_fire_distance", 1000)
go.property("min_follow_distance", 0)
go.property("max_follow_distance", 100)
go.property("min_retreat_distance", 0)
go.property("max_retreat_distance", 0)
local NORTH = vmath.vector3(0, 1, 0)
function init(self)
msg.post('.', 'acquire_input_focus')
self.correction = vmath.vector3()
self.time_until_next_bullet = 0
end
function update(self, dt)
local playerPos = go.get_position('/heroi')
local enemyPos = go.get_position()
local distance_to_player = vmath.length(playerPos - enemyPos)
if playerPos.x > enemyPos.x then
sprite.set_hflip("#slime_enemy", true)
else
sprite.set_hflip("#slime_enemy", false)
end
local direction = 0
if distance_to_player >= self.min_follow_distance and distance_to_player <= self.max_follow_distance then
direction = 1
elseif distance_to_player >= self.min_retreat_distance and distance_to_player <= self.max_retreat_distance then
direction = -1
end
direction = 1
local angle = -math.atan2(playerPos.x - enemyPos.x, playerPos.y - enemyPos.y) -- Calcula a direção angulo do jogador em relação ao inimigo
local rot = vmath.quat_rotation_z(angle) -- Marca como alvo a posição calculada
go.set_position(enemyPos + self.correction + vmath.rotate(rot, NORTH) * self.speed * dt * direction) -- Calcula a posição do slime atraves do alvo marcado e velocidade do slime
local props = self.correction + vmath.rotate(rot, NORTH) * self.speed * dt * direction
print('rot ------- ',rot)
print('props --------- ',props)
self.correction = vmath.vector3()
if self.rate_of_fire > 0 then
if distance_to_player >= self.min_fire_distance and distance_to_player <= self.max_fire_distance then
self.time_until_next_bullet = self.time_until_next_bullet - dt
if self.time_until_next_bullet <= 0 then
self.time_until_next_bullet = 1 / self.rate_of_fire
factory.create( '#slime_projetil', props, rot)
msg.post(".", "slime_projetil")
end
end
end
end
The problem is that when I pass the player’s position (x, y), factory.create () is not updating according to the player’s position and does not shoot