Fire a bullet towards a targetMonkey Forums/Monkey Beginners/Fire a bullet towards a target
We have a game where the student can fire a bullet from the right hand side of the screen. There is also a cross hair. The cross hair uses the mouse x and y.
He has managed to get it to start at the right y position and fire across. He wants to be able to fire the bullet towards the zombie from the right hand side of the screen using the cross hair location.
At present he just has a method called move that subtracts from x and y stays constant. (So bullet moves across from left to right in a straight line) I get that really it's a maths question to work out the angle and there the amount of y it needs to move each time.
At the moment he just has a list of bullets and each one it updates the x and y at present.
Any ideas on how to work out how much y it should move each time to reach the target?
Local dx:Float = zombieX - playerX Local dy:Float = zombieY - playerY Local dist:Float = Sqrt( dx * dx + dy * dy ) 'Sanity check to avoid possible division by zero If( dist < 0.001 ) Then dist = 0.001 'normalized value Local normDx:Float = dx / dist Local normDy:Float = dy / dist
in your game loop:
Local speed:Float = 10.0 bulletX += normDx * speed bulletY += normDy * speed