movement - Bullet direction in c# -
i trying make game have turret rotates mouse position, , on click firing bullets towards mouse position. problem using method (below) doesn't make bullets go straight, since can go in up, down , 45 degree diagonal direction. also, can't figure out way make bullets continue direction after reaching mouse position. suspect whole approach problem may wrong... (as i'm new c#). appreciated, thank you.
private void timer1_tick(object sender, eventargs e) { int x = picturebox1.location.x; int y = picturebox1.location.y; if (picturebox1.location.x < cursor.position.x - picturebox1.width / 2) { x = x + 3; picturebox1.location = new point(x, y); } if (picturebox1.location.x > cursor.position.x - picturebox1.width / 2) { x = x - 3; picturebox1.location = new point(x, y); } if (picturebox1.location.y > cursor.position.y - picturebox1.height / 2) { y = y - 3; picturebox1.location = new point(x, y); } if (picturebox1.location.y < cursor.position.y - picturebox1.height / 2) { y = y + 3; picturebox1.location = new point(x, y); } }
you can quite solve problem regards direction holding bullets velocity in array:
int[] bulletvelocity = {vx, vy};
where vx velocity in x direction , likewise vy y. these determine how change bullets x , y position @ each timer tick.
to make them fly towards bullet need register click position , hold bullet in question. 1 solution have bullet class
, have point
object within can set mouse click position.
you use point
set initial velocity bullet, e.g. x , y proportionate different in x , y components of bullet , click.
Comments
Post a Comment