How do I get the bullet to come out of the player and twords the direction of the clicked area JS
I have been working on a game and have been trying to get shooting working. It has been all working except for making a bullet shoot in the direction of the clicked area. Anyone know how to fix this?
Yay I get to talk about math for once.
If we want the bullet to move to the cursor, we can just move it by (X, Y). But that's not what we want.
Instead, we can move it in the same direction as the hypotenuse, but make the actual distance it travels smaller. Then, we can keep doing that every frame, and it'll look like it's moving towards the cursor!
But how do we do that? Well, first we want to scale the hypotenuse down to a size of 1. To do that, we can divide the size of each part of the triangle by the size of the hypotenuse. When we do that, the proportions stay the same, but the hypotenuse goes to 1 (since you're dividing it by itself).
Then you can scale (multiply) that to something manageable. Really that's up to taste.
Right now, your code isn't structured to handle bullet-specific variables. We can change that though. Change your code to look something like
Then we can assign the proper movement deltas after we make
Then when you update the bullet, move it by (b.dx, b.dy). You should also multiply by dt() to make sure the velocity is consistent.