So do i change it back to math.deg?
So that just does the same thing in the op video
Yes, I’m messing around with the HingeConstraint
because I don’t really know how to set it up but I got the angle working
local mouseParams = RaycastParams.new()
mouseParams.FilterType = Enum.RaycastFilterType.Exclude
mouseParams.FilterDescendantsInstances = {Character}
local function GetMouseHit(depth, customParams)
local p = UserInputService:GetMouseLocation()
local ray = Camera:ViewportPointToRay(p.X, p.Y)
local rcResult = workspace:Raycast(ray.Origin, ray.Direction * depth, customParams or mouseParams)
return rcResult or ray.Origin + ray.Direction * depth
end
local HRP = Character:WaitForChild("HumanoidRootPart")
UserInputService.InputChanged:Connect(function(input, event)
if event then return end
if input.UserInputType == Enum.UserInputType.MouseMovement then
local worldPos = GetMouseHit(50)
if typeof(worldPos) == "RaycastResult" then worldPos = worldPos.Position end
local objSpace = HRP.CFrame:PointToObjectSpace(worldPos).Unit
local angle = math.deg(math.atan2(objSpace.Z, objSpace.X))
print(angle)
end
end)
Also, I believe this would also work with just the location of the mouse on screen using UserInputService:GetMouseLocation()
instead of raycasting it to 3d space.
Edit: fixed -objSpace.Z
to objSpace.Z
Edit 2: It doesn’t work with just 2d space, because you need it to point to 3d space
What is objspace variables defined as?
I just edited the script above, I took out a snippet of it and forgot some parts sorry
But the objspace should be the same as your script, I was just using the player’s HumanoidRootPart
So its the Humanoid Root Part CFrame?
Yes, in your case that’d be the Tank’s Attachment CFrame
Ok yeah, got it! il make it work with my system. Il get back to you tommorow! thanks
Hey I’ve found someone with the same turret issue as you and they did this to use CFrame
instead
I have also finished repro, however even after setting collision to false the HingeConstraint
is still physically affect my player’s character. I’d suggest you change to this approach as HingeConstraint
is very annoying to deal with whereas setting the CFrame
will be super easy.
And instead of changing the rotation of the CFrame
it will be better to change its direction. It’d be achieved by using CFrame.lookAt or CFrame.fromMatrix
reproturret.rbxl (48.7 KB)
I will try both soloutions. I will get back to you tommmorow, to see whats better
Ok, so i tried both hinge and CFrame, with the cframe, tank tried to ascend, and with the hinge, it kinda worked, but its still really inaccurate.
Code:
local Camera = workspace.CurrentCamera
local mouseParams = RaycastParams.new()
mouseParams.FilterType = Enum.RaycastFilterType.Exclude
local Character = Players.LocalPlayer.Character
mouseParams.FilterDescendantsInstances = {Character}
local function GetMouseHit(depth, customParams)
local p = UIS:GetMouseLocation()
local ray = Camera:ViewportPointToRay(p.X, p.Y)
local rcResult = workspace:Raycast(ray.Origin, ray.Direction * depth, customParams or mouseParams)
return rcResult or ray.Origin + ray.Direction * depth, ray.Direction
end
UIS.InputChanged:Connect(function(input, event)
if event then return end
if input.UserInputType == Enum.UserInputType.MouseMovement then
local worldPos = GetMouseHit(50)
if typeof(worldPos) == "RaycastResult" then
worldPos = worldPos.Position
end
local objSpace = script.Tank.Value.gun.Value.gun.Attachment0.WorldCFrame:PointToObjectSpace(worldPos).Unit
local angle = math.deg(math.atan2(objSpace.Z, objSpace.X))
print(angle)
script.Tank.Value.gun.Value.Yaw.TargetAngle = angle
end
end)
And here is the result:
Does the angle fluctuate like the turret’s rotation? It might be because of some properties in the HingeConstraint
that causes this instead.
Also, you might want to use RunService.Heartbeat
instead, because when you move the tank, you can move it without moving the mouse so it won’t update. Or you can bind the tank movement to the function as well
You should print the angle from the script and see if it’s accurate and then check with the HingeConstraint
’s TargetAngle
and see if they are the same. If they are the same then it’s probably because of some HingeConstraint
properties
Why not just get the CFrame of mouse, then use CFrame.lookat(turret.Position, Vector3.new(x position of the mouse, turret Y position (to prevent from going up and down), Z position of the mouse))
So, there were too many pieces missing from your rbxm files to recreate your exact setup (remote events, some ‘mouses’ thing expected to be in ReplicatedStorage, etc.) so I made a dead simple LocalScript to aim the gun, just to show you that your atan2 setup will work, and that Z and Y are indeed the correct values to extract for the way you wrote your code. You could use X and Z if you had another reference attachment on the tank that is oriented to the tank, with its own Y axis pointing up. It’s because the turret hinge needs its X axis pointing up that you have Z and Y in your code, as you’re using that attachment as the reference frame (also fine to do).
I did have to change a bunch of other things to get this to work:
- I added a ServerScript just to set Nefwork ownership to whomever is in the seat, which is now a VehicleSeat (not necessary, but wanted to rule out this being part of the problem)
- I had to unanchor everything in the tank, so that network ownership can be set (assuming you have a script doing this also someplace)
- I made the the gun not collide so that wouldn’t hit my avatar and stop
- I changed the orientation of the hinge attachments, and order. The order was backwards, Attachment1 should be on the moving gun part, not the stationary pivot piece. Attachments are now totally aligned and with the reference axes in the direction they need to be to use the atan2 math without extra angle offsets having to be added in code.
- The test script is a LocalScript, which is really what it should be so that you don’t have client-server-client round trip lag hurting responsiveness.
I’ve attached the sample file I made so you can see how the hinge attachments should be set up and oriented.
tonk.rbxl (245.2 KB)
You can check off “Massless” on the turret parts, or use CustomPhysicalProperties to reduce the density. You should probably also put a reasonable MaxAngularVelocity on the HingeConstraint motor settings so that it can’t spin crazy fast.
Your tank has too things contributing to this problem: The turret is not balanced, which makes it wobble. But even if you balanced the turret at the pivot point, if the tank body’s mass isn’t much higher than the turret’s mass, or the tank doesn’t have enough friction with the ground, the body can still counter-rotate (Newton’s 3rd law of motion).
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.