Hey there! So I’m making a game similar to diep.io with GUI objects, I already have the player movement, mouse rotation, and cannon working, but can’t seem to understand how to make it so the cannon shoots the bullets to the mouse position.
You can get the mouse position using UserInputService:GetMouseLocation. Then you could use the math.atan2 function to calculate an angle from the X and Y components of the difference between the mouse position and character position.
Can you show an example in code of how to use that?
Yeah, sure, I think the explanation I gave is pretty hard to understand.
local Workspace = game:GetService("Workspace")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local Camera = Workspace.CurrentCamera
-- Assuming you have all of your GUI objects defined as variables...
local function UDim2toVector2(input)
return Vector2.new(
input.X.Offset + Camera.ViewportSize * input.X.Scale,
input.Y.Offset + Camera.ViewportSize * input.Y.Scale
)
end
RunService.RenderStepped:Connect(function() -- Using RenderStepped for quick response
local MousePos = UserInputService:GetMouseLocation()
local CharPos = UDim2toVector2(CharFrame.Position).Unit
local PosDiff = MousePos - CharPos
local DiffAngle = math.atan2(PosDiff.X, PosDiff.Y)
-- And then you can use the DiffAngle to tilt the character!
-- math.atan2 returns results in radians, so we need to convert it to degrees for the UI rotation.
CharFrame.Rotation = math.deg(DiffAngle)
end)
What I’m understanding from this code is that you are trying to rotate the character to the mouse, am I right?
If so then I already have the character rotating to the mouse I just need to spawn a frame at the character’s position and so it would move smoothly to the mouse position.
RunService.RenderStepped should do the trick. Or are you looking to make the character move smoothly like TweenService?
Oh… sorry.
-- When your event gets fired...
Remotes.FireBullet:FireServer(string.pack(">h", math.floor(MouseAngle)))
-- When the server recieves it...
-- I don't know what you're using for multiplayer, so I'll make something up.
local CharPos = Player:GetAttribute("CharPos")
MouseAngle = string.unpack(">h", MouseAngle)
-- Assuming you have a replicator remote event
Replicator:FireAllClients("NewBullet", Player, CharPos, Angle)
-- Back on the client...
local NewBullet = Instance.new("Frame")
-- Customize it how you like
-- Get a new position from the angle
NewBullet:TweenPosition(NewPos, Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 2)
string.pack helps send the data in a more compact format.
First of all, thank you for trying to help. Secondly you got it all wrong, I don’t need character movement nor multiplayer, I got the player shooting bullets but currently he shoots only to the right even if I rotate him and not from the cannon on the player. For example, I want it to be that if the cannon on the player is facing up then the bullet will shoot up and so forth.