✓ Accepted answer
You can use the Mouse.Hit CFrame to get the location within 3D space, then use .p to return the Vector3 Position component of the CFrame.
Applying the .Move signal of Mouse, we can catch when we move our cursor to essentially create a lookVector built from the cursor's X,Z coordinates for perpetual rotation. Combining the numerical constant of Y with the Turret Position, we can finally have the Turret rotate in three-hundred-sixty degrees where it stands, by adjusting it's CFrame accordingly.
Note:
When Scripting projectiles, it is always recommended to use DebrisService's :AddItem() method to schedule removal of the projectile after a given window. If we do not do this, we'll flood workspace with unnecessary parts.
A couple more things:
Setting or comparing properties should always be done via Enums, it is a deprecated practice to use Strings; it is no-longer supported by ROBLOX.
With creating new Instances through Instance.new(), you shouldn't provide the parental argument as it is also considered deprecated, and can cause potential performance issues. You should set the .Parent property outside the constructor—since you're also modifying the Object, you should set the parent last.
When dealing with InputObjects, you should always debounce the GameProcessed parameter, as it allows core services to process the InputObject with a higher priority. This will prevent important preset keybindings from being interrupted.
local UserInputService = game:GetService("UserInputService")
local Debris = game:GetService("Debris")
local Player = game:GetService("Players").LocalPlayer
local Cursor = Player:GetMouse()
local Turret = workspace.Turret
local Gun = Turret.Gun2
local function FireProjectile(InputObject, GameProcessed)
if (GameProcessed) then return end
if (InputObject.KeyCode == Enum.KeyCode.F) then
local Projectile = Instance.new("Part")
Projectile.Size = Vector3.new(1,1,2)
Projectile.Material = Enum.Material.SmoothPlastic
Projectile.CanCollide = false
Projectile.CFrame = Gun.CFrame
Projectile.Parent = workspace
local BodyVelocity = Instance.new("BodyVelocity")
BodyVelocity.Velocity = Projectile.CFrame.lookVector * 100
BodyVelocity.Parent = Projectile
Debris:AddItem(Projectile, 5)
pcall(function()
Projectile.Touched:Connect(function(Hit)
local Humanoid = Hit.Parent:FindFirstChildOfClass("Humanoid")
if (Humanoid and Projectile) then
Humanoid:TakeDamage(Humanoid.Health)
Projectile:Destroy()
end
end)
end)
end
end
Cursor.Move:Connect(function()
local CursorPosition = Cursor.Hit.p
local TurretPosition = Turret.Position
local LookVector = Vector3.new(CursorPosition.X,TurretPosition.Y,CursorPosition.Z)
TurretPosition.CFrame = CFrame.new(GunPosition, LookVector)
end)
UserInputService.InputBegan:Connect(FireProjectile)
If this helps, don't forget to accept this answer!
2 comments
You're very vague. Can you please explain your problem? Also, try 'tabbing' your lines of code. Press tab on the pieces of code that are wrapped around a function or an if statement, please. It makes it easier for us to read.
ok i will try again