I want a make a missile which will follow the player, the creating part is ok. However, I can’t make missiles move towards player’s position. How can I do it? I played with the code little bit like changing the dir property to player’s position but not works… Here is an example of my move script
function update(self, dt)
if self.moving then
local pos = go.get_position()
pos = pos + self.dir * self.speed * dt
go.set_position(pos)
if pos.y < 0 then
go.delete()
end
end
end
Hi @baloglub!
In your example, how do you update the self.dir
variable?
Actually in the code that I shared, I did not updated. But i tried this code
function update(self, dt)
if self.moving then
self.dir = go.get_position("/player")
local pos = go.get_position()
pos = pos + self.dir * self.speed * dt
go.set_position(pos)
if pos.y < 0 then
go.delete()
end
end
end
What you have there isn’t really a direction, but the object (player) position.
To make a direction from your object to the player, you need to take the different between the two objects positions.
self.dir = go.get_position("/player") - go.get_position()
-- to make sure the direction is in 2D (the player and object may have different Z values)
self.dir.z = 0
-- to make the direction vector have a length of 1
self.dir = vmath.normalize(self.dir)
4 Likes