Laser going in wrong direction?

Hello, I am trying to make a spaceship, and it will shoot lasers. The lasers are going in the wrong direction though, well sometimes. The goal is for the lasers to shoot at the mouse, and half the time they do, but the other half of the time they just go in a random direction.

DevForum isn’t letting me add a video but it’s like this:

I am at 0,0,0. I shoot to 15,18,19. The bullets go to 48,6,3

This is my code:

local Players = game:GetService("Players")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local Player = Players.LocalPlayer
local Char = script.Parent

local Mouse = Player:GetMouse()

local Laser = ReplicatedStorage:WaitForChild("Assets"):WaitForChild("LaserProjectile")
local LaserShooters = {Char:WaitForChild("LLaserShooter"), Char:WaitForChild("RLaserShooter")}

local Mouse = Player:GetMouse()
local MousePosition = Mouse.Hit.Position

local DB = false

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Space then
		DB = true
		local LaserClone = Laser:Clone()

		local SelectedShooter = LaserShooters[math.random(1, #LaserShooters)]

		local BodyVelocity = Instance.new("BodyVelocity", LaserClone.Root)
		LaserClone.Root.CFrame = CFrame.new(LaserClone.Root.Position, Mouse.Hit.Position)
		local PValue = 0.5
		BodyVelocity.Velocity = LaserClone.Root.CFrame.LookVector * 50 --Vector3.new(50,50,50)
		BodyVelocity.MaxForce = Vector3.new(1000,1000,1000)
		
		BodyVelocity.P = Vector3.new(PValue,PValue,PValue)

		LaserClone.Parent = workspace
		LaserClone:MoveTo(SelectedShooter.Position)

		MousePosition = Mouse.Hit.Position 

		LaserClone:PivotTo(CFrame.lookAt(LaserClone.Root.Position, MousePosition))

		task.wait()
		DB = false
	end
end)

I can provide additional information if necessary.

Any help is greatly appreciated. Thank you!

1 Like

I think its the way you’re setting the CFrame of the LaserClone.Root . You’re setting it to look at the mouse’s hit position when you fire it however, if the mouse moves after firing but before the laser is actually created and gets its velocity, it might cause the laser to go in an unexpected direction

Try this>>

local Players = game:GetService("Players")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local Player = Players.LocalPlayer
local Char = script.Parent

local Laser = ReplicatedStorage:WaitForChild("Assets"):WaitForChild("LaserProjectile")
local LaserShooters = {Char:WaitForChild("LLaserShooter"), Char:WaitForChild("RLaserShooter")}

local Mouse = Player:GetMouse()
local MousePosition = Mouse.Hit.Position

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Space then
		local LaserClone = Laser:Clone()

		local SelectedShooter = LaserShooters[math.random(1, #LaserShooters)]

		local BodyVelocity = Instance.new("BodyVelocity", LaserClone.Root)
		MousePosition = Mouse.Hit.Position -- Update MousePosition here
		LaserClone.Root.CFrame = CFrame.new(LaserClone.Root.Position, MousePosition)
		local PValue = 0.5
		BodyVelocity.Velocity = LaserClone.Root.CFrame.LookVector * 50 --Vector3.new(50,50,50)
		BodyVelocity.MaxForce = Vector3.new(1000,1000,1000)
		
		BodyVelocity.P = Vector3.new(PValue,PValue,PValue)

		LaserClone.Parent = workspace
		LaserClone:MoveTo(SelectedShooter.Position)

		LaserClone:PivotTo(CFrame.lookAt(LaserClone.Root.Position, MousePosition))
	end
end)
1 Like

It’s a bit cleaner, but the same problem still appears.

1 Like

Try moving the line MousePosition = Mouse.Hit.Position inside the InputBegan event, just before you use it to set the CFrame like this for example

local Players = game:GetService("Players")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local Player = Players.LocalPlayer
local Char = script.Parent

local Laser = ReplicatedStorage:WaitForChild("Assets"):WaitForChild("LaserProjectile")
local LaserShooters = {Char:WaitForChild("LLaserShooter"), Char:WaitForChild("RLaserShooter")}

local Mouse = Player:GetMouse()

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Space then
		local LaserClone = Laser:Clone()

		local SelectedShooter = LaserShooters[math.random(1, #LaserShooters)]

		local BodyVelocity = Instance.new("BodyVelocity", LaserClone.Root)
		local MousePosition = Mouse.Hit.Position -- Update MousePosition here
		LaserClone.Root.CFrame = CFrame.new(LaserClone.Root.Position, MousePosition)
		local PValue = 0.5
		BodyVelocity.Velocity = LaserClone.Root.CFrame.LookVector * 50 --Vector3.new(50,50,50)
		BodyVelocity.MaxForce = Vector3.new(1000,1000,1000)
		
		BodyVelocity.P = Vector3.new(PValue,PValue,PValue)

		LaserClone.Parent = workspace
		LaserClone:MoveTo(SelectedShooter.Position)

		LaserClone:PivotTo(CFrame.lookAt(LaserClone.Root.Position, MousePosition))
	end
end)

That still didn’t change anything. I think the problem may be because the player’s character’s cframe is pretty large, and may mess up the mouse position. Do you know if there is a way to make the mouse not detect the character?

set mouse.TargetFilter to the player’s character. You should also look into Raycasting.

Ye you can make the mouse not detect the character by using the Mouse.TargetFilter property. Like this

local Players = game:GetService("Players")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local Player = Players.LocalPlayer
local Char = script.Parent

local Laser = ReplicatedStorage:WaitForChild("Assets"):WaitForChild("LaserProjectile")
local LaserShooters = {Char:WaitForChild("LLaserShooter"), Char:WaitForChild("RLaserShooter")}

local Mouse = Player:GetMouse()
Mouse.TargetFilter = Char -- Ignore the character

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Space then
		local LaserClone = Laser:Clone()

		local SelectedShooter = LaserShooters[math.random(1, #LaserShooters)]

		local BodyVelocity = Instance.new("BodyVelocity", LaserClone.Root)
		local MousePosition = Mouse.Hit.Position -- Update MousePosition here
		LaserClone.Root.CFrame = CFrame.new(LaserClone.Root.Position, MousePosition)
		local PValue = 0.5
		BodyVelocity.Velocity = LaserClone.Root.CFrame.LookVector * 50 --Vector3.new(50,50,50)
		BodyVelocity.MaxForce = Vector3.new(1000,1000,1000)
		
		BodyVelocity.P = Vector3.new(PValue,PValue,PValue)

		LaserClone.Parent = workspace
		LaserClone:MoveTo(SelectedShooter.Position)

		LaserClone:PivotTo(CFrame.lookAt(LaserClone.Root.Position, MousePosition))
	end
end)

I guess targetfilter didn’t work either, the lasers still move in random places

I believe the reason why your code is not working is because you are moving the laser way too late. You’re doing all the calculations when it is still at it’s default position, but then you move it to workspace and everything gets offset. Try this instead and lmk if it works :smiley: (Note this is untested):

local Players = game:GetService("Players")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local Player = Players.LocalPlayer
local Char = script.Parent

local Mouse = Player:GetMouse()

local Laser = ReplicatedStorage:WaitForChild("Assets"):WaitForChild("LaserProjectile")
local LaserShooters = {Char:WaitForChild("LLaserShooter"), Char:WaitForChild("RLaserShooter")}

local Mouse = Player:GetMouse()

local DB = false

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Space then
		DB = true
		local MousePosition = Mouse.Hit.Position
		local LaserClone = Laser:Clone()
		
		local SelectedShooter = LaserShooters[math.random(1, #LaserShooters)]
		LaserClone.Parent = workspace
		LaserClone.Root.CFrame = CFrame.new(SelectedShooter.Position, MousePosition)

		local BodyVelocity = Instance.new("BodyVelocity")
		BodyVelocity.Parent = LaserClone.Root
		
		BodyVelocity.Velocity = LaserClone.Root.CFrame.LookVector * 50
		BodyVelocity.MaxForce = Vector3.new(1000,1000,1000)
		BodyVelocity.P = Vector3.new(0.5,0.5,0.5)

		LaserClone.Root.CFrame = CFrame.new(LaserClone.Root.Position, MousePosition)

		task.wait()
		DB = false
	end
end)
1 Like

Thank you all so much, it works now!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.