I’ve implemented a weapon that’s capable of firing, aiming and all that a weapon does, but I’ve ran into a big problem for me where the gun doesn’t align with the crosshair while moving, for example when you jump the weapon points straight up. I’ve set up aim offsets which help a little bit but not a big difference, and I’ve tried looking for tutorials but the code is either too messy as in nodes out of view and wires across the screen going back and forth, or they don’t help me at all. If anyone has any idea on how I could rotate the mesh and align it to the crosshair while firing the weapon would be a big help to me. I’m new at weapons and started implementing them 2 days ago.
Instead of doing that you can shoot using raycasts. So the ray cast will come out from the center of the follow camera if you are making a FPS game. You can use particle effects to show the muzzle flash and shoot with raycasts to shoot the AI or if you are making a multiplayer game, other players.
You can follow this tutorial:
Thx for replying, the video you sent will be helpful to me but it’s not what I meant. The problem isn’t the projectiles, its the mesh/weapon itself. For example if the player jumps the socket follows the bone and rotates up for a bit then back down, I was looking for a way to counteract that. I am new to this so sorry if I’m not understanding what you say sometimes.
Forgive me for not being patient. Are you still helping?
Good News! After a bit of experimenting with find look at rotation and the line trace you showed me, the mesh aligns with the center no matter what and I subtract 90 from the z axis to rotate it from the right to the middle! Thx for your help!
Hey could you post an image how you did it?
In general idea for accurate aiming is this:
- trace line in forward vector of camera that goes trough cross-hair. Find where line trace has hit result.
- now you have. Origin of rifle, its forward vector, and point in 3d it should aim.
- find look at rotation for your gun.
Some things to consider:
- you are making game, above solution is mathematically accurate solution, but those not always “feel” right. Sometimes you will get weird unnatural (but correct) rotations of gun.
- if you are making first person shooter game, do not bother with accurately rotating gun. Limit maximum rotation to about 15 deg, then use aim offset to sway that gun a bit.
- Whole aiming is just cosmetic effect (like feet not sliding). Depending on your game, how advanced project is etc. Is that stuff really that important for your game, or important enough now?
So focus on game-play, movement, etc. feet placement, gun swaying, accurate animations, are usually for late stages of making game.