eGGzy Posted March 13, 2014 Share Posted March 13, 2014 I'm making a tower defense game, and I have a tower shooting at enemies. Problem is that bullets don't really hit the enemy. I'm currently using moveToObject method for bullet to enemy logic, but none methods in docs actually track the destination object. Is there a workaround for this or is there a bug in the code.Here's the code so far. Link to comment Share on other sites More sharing options...
drhayes Posted March 13, 2014 Share Posted March 13, 2014 You should probably shoot at where the enemy is going to be, make the bullets a lot faster, or fake it using a tween. Shooting where the enemy is going to be would just use some math: figure out how far the tower is from the enemy, calculate how much time it'd take at your bullet's velocity, multiply that by the enemy's velocity, then aim at that spot instead. If the bullet's moved faster and, maybe, had larger hit areas it's okay that they lag behind the enemy – they'll hit anyway. If you fake it using a tween then the bullets would follow the enemy, curving in their trajectories. Might not be what you want. I guess you could also fake it a bit: don't depend on the bullet sprites actually hitting the enemy sprites, just remove some of their health on a timer and animate everything correctly. Link to comment Share on other sites More sharing options...
eGGzy Posted March 13, 2014 Author Share Posted March 13, 2014 I'm fine with bullets curving to hit the enemy, how would I do that with tween? Link to comment Share on other sites More sharing options...
drhayes Posted March 14, 2014 Share Posted March 14, 2014 Ah, sorry, I'm a little new to Phaser myself. Maybe the Phaser Tween isn't exactly what you want since it looks like you set the destination and time it takes once and then start it running. Might be better to, every frame (in update perhaps), calculate the angle to the target and advance the bullet a small fraction of that amount. If you're using Phaser 2.0 and Arcade physics then moveToXY in your bullet's update function would probably help. You might need to use angleToXY instead and then call velocityFromAngle. Don't forget to convert what comes back from angleToXY (radians) to degrees before calling velocityFromAngle. You can call all these methods like this: game.physics.moveToXY, I believe. Link to comment Share on other sites More sharing options...
Recommended Posts