Factory spawning game object from 0, rather than from the player

I’ve created a factory object in my player, to shoot a bullet at a distance ahead of the player, which works fine. However, when I add animations to this, it first spawns the object from 0/the corner of my map, then travels to the desired distance. Removing the animation seems to allow for the bullet to spawn where I want it, albeit it teleports there rather than animates a path.

I have tried disabling the a sprite until it collides with the player, then re-enabling it, but had no luck.

The code I use has come from the bullet tutorial, which seems to work, and based on what I’ve read, should work, so I am unsure of why the bullet is animating from the corner of the map, rather than the player

if action_id == hash("fire") and action.pressed and ammo>0  then
		local pos = go.get_position()
		pos.x = pos.x + 50
		local rocket_id = factory.create("#rocketfactory",pos)
		ammo=ammo-1
		go.animate(rocket_id, "position.x", go.PLAYBACK_ONCE_BACKWARD, 1, go.EASING_LINEAR,1)

The 4th argument for go.animate() is “to”, i.e. the value you want to animate to. Currently you are animating the rocket to x position 1. This doesn’t seem right, as it’s a fixed value that has nothing to do with the player’s position.

I suggest you change “position.x” to “position” (so that both x and y will be correct), and change “to” from 1 to the player’s position (which should be a vector rather than a number).

1 Like

Like this?

go.animate(rocket_id, "position", go.PLAYBACK_ONCE_BACKWARD, self.dir, go.EASING_LINEAR, 1)

self.dir comes from:

self.dir = vmath.vector3() 

But I’m still having the same issue, I’ve also tried using the vectors from the API reference, and had no luck there. If i use vmath.vector3() or another similar vector, no animation seems to occur

No, the “to” variable should be a world position where you want the object to animate towards.

By using vmath.vector3(), you’ll get (0,0,0), and you already said you didn’t want the object to animate to (0,0,0).

local target_pos = calculate a world position you want to target
go.animate(rocket_id, "position", go.PLAYBACK_ONCE_BACKWARD, target_pos, go.EASING_LINEAR, 1)
1 Like