How can i make my projectile follow my mouses x and z axis?

Yeah i noticed that too. His code makes no sense

Lets forget about him for a second, Do you know what my problem is and if so can you help? Ive been trying to change and experiment with code but i just cant figure it out.

I’m not too sure what you are trying to achieve, it looks like the issue is that when you fire it too far, the bullet goes too high, but it seems like the gun is still working as expected?

Would limiting the range be a solution?

1 Like

Not really the problem right now what i mean is i only want my projectiles assembly velocity to count the x and z axis. The y axis already scales with magnitude

Which will solve the problem of the projectile bugging out in the void

2 Likes

It might be a good idea to use Camera:ViewportPointToRay() (or ScreenpointToRay) to get a ray and do a raycast using it. Make the ray have a limited range, and if it hits nothing, then the hit position would be the end of the ray basically

2 Likes

Ill try and get back to you later. Thanks

1 Like

You already have the direction. You just need to normalize it and then create a new Vector excluding the y-component.

local Direction = (Mouse.Hit.Position - Pistol.FirePart.Position)

The code you have above gives you a Vector starting from the FirePart.Position and ending at Mouse.Hit.Position. The problem here is that this Vector is not just a direction, it also holds a magnitude, or length, greater than 1.

To get the actual direction Vector, you need to normalize the Vector by adding .Unit.

local Direction = (Mouse.Hit.Position - Pistol.FirePart.Position).Unit

Now, you stated you only wanted the x and z components, and it’s as simple as creating a new Vector with the X and Z components of Direction and setting the Y component to zero or a random constant depending on what you need.

local Direction = (Mouse.Hit.Position - Pistol.FirePart.Position).Unit
local DirectionXZ = Vector3.new(Direction.X, 0, Direction.Z)

And using that vector when calculating your force achieves the behavior you want.

1 Like

Thanks you soooo sooo soooooo much man, This is excatly what ive been needing i just couldnt wrap my head around it. Hope you have a great day.

1 Like

I can understand your frustration but you need to respect everybody even if it’s online.

But now the direction is 1 so its very short. anyway i could still scale it with the mouse position?

new code:

if Cooldown == false then
		Cooldown = true
		
		local Range = 10
		
		local Direction = (Mouse.Hit.Position - Pistol.FirePart.Position).Unit
		local DirectionXZ = Vector3.new(Direction.X, 0, Direction.Z)
		
		local NewBullet = Bullet:Clone()
		NewBullet.Position = Pistol.FirePart.Position
		NewBullet.Parent = workspace
		
		local Magnitude = 0.01 + DirectionXZ.Magnitude * 0.01
		
		local Duration = Magnitude
		
		local Gravity = workspace.Gravity / 2
		
		local Force = DirectionXZ / Duration + Vector3.new(0, Gravity, 0) * Duration
			
		NewBullet:ApplyImpulse(Force * NewBullet.AssemblyMass)
		
		Debris:AddItem(NewBullet, 10)
		
		task.wait()
		Cooldown = false

Yeah. Just remove the .Unit. It’ll scale with the mouse position.

Or, you can keep the .Unit and multiply DirectionXZ by some random large value. It will make the bullet travel farther but the distance will always be fixed no matter how far the player clicked.

It’s up to you what you want, but both options should help.

1 Like

Im not even sure at this point what to do it still bugs out from the skybox. Like if i click the skybox it the projectile goes to the max range