Mouse and Bullets not aligned

Hello again, I would like my weapons to accurately point towards the mouse.
Current architecture:

--in weapon/server script
local mousePos, mouseHold = game.ReplicatedStorage.mouseInfo:InvokeClient(player)
gun.AIMING.mover.CFrame = mousePos

--in local script
mouseInfo.OnClientInvoke = function()
	return mouse.Hit, holdingClick
end

mover is a bodygyro that is on a part just in front of the gun’s barrel.


But you see, they really don’t align.


https://gyazo.com/21bac453d7dca2e0b6cd96108a999417


I would rather avoid using raycasts on the client because mouse.Hit is basically that so it would most likely not solve my problem.

And I really don’t know what to do :T

1 Like

The bullet doesn’t go from camera towards the target, it goes from launcher. It’s possible for it to hit something before the mouse.Hit due to that.

Could just be that the cursor image itself is not aligned to the center. Have you tried without modifying the cursor?

3 Likes

@Legoracer
How would you recommend I remake it? Raycast from the camera to get a position, and then get a direction from the gun to the position and slap that into the body gyro?

@DesiredFlamingFire
I have not tried that yet, I will test that soon. Thank you!

Where is the BodyGyro located? It isn’t centered with the projectile, it will point to the target as expected, but the projectile will not and will go a different path. Even if the gun is the mover, the bounding box may not be centered with the projectile.
Rough example with Microsoft Paint:

I think what’s happening here is that your mouse.Hit is actually very far away into space. It seems out of allignment because the line from gun > mouse position is obscured by whatever the projectile is hitting. It’s not getting the chance to reach the mouse position.

PS: If you anchor your projectile + hide it instead of simply destroying it then the smoke trail won’t instantly disappear like that

1 Like

What I’ve seen done in games before is having a cursor for the mouse as normal but adding something like a Beam from the weapon to the actual collision point. If this isn’t desirable then I’d recommend moving the camera closer to the weapon.

BodyGyro is located infront of the barrel, exactly where the projectiles are spawned.

@yelowfat
What would you recommend me to do instead of mouse.Hit then?

Maybe it would be better to create your directional unit vector by doing (mover.Position - mousePos.Position).unit instead of (I assume) using the directional vector of the turret. To make sure projectiles don’t go sideways compared to your turret (because your turret could be pointing one way and the missile could go another way), get the angle between the turret’s lookvector and (mover.Position - mousePos.Position).unit and make sure the angle is very small.

svg

My issue with that is that it would require the player to stop their camera completely for a second before being allowed to shoot.

It also wouldn’t fix the problem, because the parallax happens even when not moving around.

Is the missile launcher’s orientation controlled by the player’s camera?

No, all weapons/turrets are controlled by the mouse.Hit property. The camera is allowed free reign, I guess.

I don’t see why the player would need to stop their camera then.

Sorry, stop their mouse. And holding a mouse still for a couple seconds is rather annoying…

Changing the mouse icon, I realize that the code is the main reason for issue:

Okay, then instead of setting the gyroscope CFrame to the mouse CFrame, then do (mover.Position - mousePos.Position).unit and use that directional vector to orient the missile launcher.

3 Likes

This?

gun.AIMING.mover.CFrame = CFrame.new(gun.Base.Position ,(mousePos.Position - aim.Position).Unit)

aim is the part that houses the mover

BTW, this does not work…

However this does:

gun.AIMING.mover.CFrame = CFrame.new(Vector3.new(0,0,0) ,(mousePos.Position - aim.Position).Unit)

And works very well. Thank you!

2 Likes