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

How can i make to so my projectile doesnt bug out when i click the sky?

Instead of following mouse.hit i want it to follow the x and z axis .What i mean is if i click straight up my projectile still applies the force it should if i clicked right beside it.

(Im not that great at explaining sry if its confusing)

My code:

local Pistol = script.Parent

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local Cooldown = false
local Debris = game:GetService("Debris")

local Bullet = game.ReplicatedStorage.Bullet

local Character = Player.Character
local HRP = Character.HumanoidRootPart

local Sound = Pistol.Sound

local Debris = game:GetService("Debris")

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

robloxapp-20240701-1546525.wmv (1.6 MB)

3 Likes

bumping my post up since no one saw

1 Like

It looked like you were clicking the void. (Maybe limit how far out you are able to shoot?) You could use a raycast to ensure a position of how far out you want it to shoot to? (Raycast from your camera to your mouse in the 3d plane? Limit the range using some math, maybe?)

1 Like

A raycast would work but if i click on the void the raycast would be nil and wouldnt shoot my projectile. I still want it to shoot if click the void. My fix would be making the projectile count the x and z axis of the mouse and the y axis would scale with the x and z coordiantes.

1 Like

You have to use Mouse.XAxis and Mouse.Orientation in order to get your projectile to follow your mouse.

1 Like

How would my code look after that? Im confused would i replace mouse.hit in direction?

1 Like

Sorry for the misunderstanding, you actually have to use Mouse.Orientation and get the Position of the orientation and face the projectile to the mouses orientation position.

I couldnt find mouse.orientation can you put it in code?

Here is your code.

Projectile:APIRoblox:LookAt(Mouse.RobloxAPI.Orientation)

I shouldve specified what i needed my bad. What i mean is when i click i want the projectile to only follow the x and z axis of the world where the mouse is. And the Y axis already scales because of magnitude. I think ill put an example of what i mean if you dont understand.

(sorry)

Okay. You can use the mouses “Handle Axis” for that.

local handleAxisX = mouse:GetAxis(X)
local handleAxisY = mouse:GetAxis(Y)
local handleAxisZ = mouse:GetAxis(Z)--Perhaps not needed

image

I get the error “Uknown Global Z”

GetAxis is not a valid function of Mouse.

I’m sorry, I can not read images at this time. May you describe the image to me?

Don’t listen to him, he is trolling people on multiple topics, I reported him to moderators

Who @Miserable_Haven? aaaaaaaaaaaaaaaaaa

Okay, I understand now. You can use the Mouse.Hit.Position and make your projectile face to the players Mouse.Hit.Orientation.

Smh, would you know a answer to my problem?

Yeah

This code makes 0 sense,

Mouse:GetAxis(X) doesn’t exist

And on other topics it’s the same


If just it was that simple to make an anti cheat…

(post deleted by author)