So I have my top-down character mechanics in place. How would I go about having a 2D crosshair follow the mouse, so that I could fire projectiles in the direction the crosshair is. It’s in a top-down perspective, so it’s basically a twin-stick shooter mechanic.
How can I create top down mouse-driven aiming? (Crosshair follows cursor, fires projectiles at cursor location)
I hope this helps shine some light on your question. If you use the section where I get the Mouse Coordinates for the Crosshair, and apply that to the direction the projectile needs to go. I believe that should do it. Let me know if I missed something.
Notes: Make sure the Crosshair Texture is drawn last within the stack to ensure it overlays every other HUD object. This BluePrint was a parent of the HUD class.
Adding Custom Crosshair & Getting Mouse X/Y
Are you asking how to make the mouse visible or how to actually fire at the mouse?
In the controller defaults:
Check my answer here:
Where am I making this blueprint, because I can’t find the HUD event.
Hmm, the show mouse cursor isn’t working for some reason. Also, I’m trying to to have the projectile fire towards the mouse, but I can’t find a way to modify its rotation/velocity towards the mouse coordinates.
You will need to create a BluePrint and make its parent the HUD class. The HUD class is under Actor class. When you go to create a new Class Blueprint, you must look under Custom Classes then Search for HUD
If you need instructions on how to do this, go to Creation via Class Viewer in the lower section of this BluePrint Creation Example.
Review Acren’s post below, simply changing the Set Rotation Node to the Spawn Actor from Class, then set its Rotation. Depending how you have your Top Down Setup, it may be relevant to set the Actors Rotation. IE, If your gun follows the Actor, Set Actor. IE, If you Actor follows the Gun, Set Projectile Rotation
Thanks man, you’re helping a ton. Any idea why my crosshair texture just draws as a white box? I have it set in the Draw Texture node, but no matter what I do, it is just a box, even though the texture is a crosshair (with alpha channel). I’ve tried messing with the tint color (changing the alpha value, etc.) and blend mode but nothing works.
Have you tried using Draw Texture Simple? Draw Texture is more complex, but gives you more control.
Draw Texture Simplesimply needs the Texture, X & Y location provided by the Setup Above.
Draw Textureneeds the Texture, X & Y, Screen W & Screen H sets Width & Height, Texture U & V can be ignored, but this moves Top Left origin in UV Space, Texture UWidth & VHeight are the scale in relation to the Width & Height, Set these to 1 usually, Blend Mode can be changed to what you desire.
Draw Texture Explained
To further elaborate the uses for Draw Texture. Texture U & V allow setting the Top Left Draw Point of Texture. So if you were using a sprite sheet or any texture with multiple Images. You would set this to start in a position within the texture, then set the Screen W & H to draw the size of the image within that area. UWidth & VHeight could be used to Tile the texture. This tells the texture spaces within the UV. Does it have 1, 2, or 3 spaces? Setting it to 2 / 2 will have the texture repeat 4 times within the given Width & Height aka UV Space. 2 / 1 will print 2, but stretch it horizontally to fill the UV Space.
That’s what I am using. It still just draws as a box (whatever the tint color is). I haven’t tried the simple version yet though. I’ll try that when I get home, but I have a feeling it won’t make a difference.
Its possible you didn’t set the UWidth & VHeight properly. Setting it to a high number will result in a color of the texture itself. Repeating it so much, its most prominent color is only shown. Setting this to 1, will tell the Texture to draw once within the UV Space. Thus showing the texture in its full clarity.
UWidth & VHeight don’t do anything, but Simple Draw Texture actually works fine. I just have to put the scale to 0.05. However, it seems slightly off from the actual mouse position.
Edit: Actually 0.04 is perfectly on center with the mouse position, and it looks better. Thanks! Now I just gotta figure out the projectile part. I can get it to fire when clicking, but I can’t get it to fire from the player position or in the direction of the mouse.
If you change the scale of the image, they wouldn’t be its true resolution. You will have to make the formula Resolution * 0.05 / 2, then subtract that from the X & Y. So resolution*Scale/2 would center the Crosshair. I can provide an example after UE4 updates.
Ah that makes sense. Thanks.
Sweet glad I could help. I learned more about the Draw Texture node just now. So the feeling is mutual. Acren below has a great example of generating a rotation based on Mouse Location. So check that out and you should be on your way.
My problem is that I can’t get the rotation to apply to the projectile, or have it spawn at the actor for that matter. Even this:
doesn’t spawn it at the actor location, only the place where it spawns.
If that is located within the Pawn Class firing that Projectile, it should work fine. I can only assume there is a problem with the Projectile itself.
Try reviewing this video for a step by step process of setting up projectiles.
Nope… still the same. It just fires in one direction and spawns in the location the player started at.
Yeah when you try to apply this into the top down blueprint it shoots the projectile only from the origin point, I am curious how this is done also. I saw a “get mouse position” function but I had no luck hooking things up. Super curious how this is done.
Is this a Custom Pawn Class made by you or using Premade Examples?
Self made. It’s basically a rolling ball from a top down perspective (see the game "Waves’). There’s a thread I made about it in the blueprint section of the forums.