Fire bullet to mouse position

Hi,

I 've setup a 3D isometric setup where I could fire bullets to mouse position. So far so good. But if I get the intersection point from mouse to ground area and then fire on 2m height it looks bad as I don’t intersect the mouse pointed place. I’m pretty sure with some math calculation I could find out the correct velocity of my bullet to intersect the mouse position. I couldn’t fire direct to this position as this fires into the ground, which isn’t what I need.

Maybe it’s to late today. Any hints? :slight_smile:

Here’s a way to think about your problem:

Let’s say you have a line between your mouse point and your camera (we’ll call it mouseToCam). From the camera’s perspective, this line (mouseToCam) is a point. This means that any object positioned on mouseToCam will be at the same screen-space coordinate as your mouse. So…

Step 1: Position your bullet some place on this line.

Hmm… but where”, you might ask.

Well, suppose you then have an XZ-plane (a flat plane with its surface pointing up) at the same y-coordinate as your bullet. When does the aforementioned line (mouseToCam) intersect with this plane? This intersection point will be where you should place your bullet.

Step 2: Compute the intersection between the XZ-plane and mouseToCam

Thanks for this idea! I’ve implemented it by adding a second detection Area for the yet existing raycast from my camera to the ground. But just in the heigth of the bullet. This way it works great! :slight_smile:
I used this hint for multi raycast detection from camera: Allow RayCast to report multiple collision points through different objects · Issue #8888 · godotengine/godot-proposals · GitHub


Here you see the two areas for detection.

But this brings up another problem. If I fire a coordinate “to near” to my character the fire direction looks weird. Maybe I’ll check if the mouse coordinate is to near to the character and in this case just fire straigh forward to the face direction.

1 Like