ok, here is a snap shot:
ok?
ok, here is a snap shot:
ok?
My bad again, math.deg
converts radians to degrees while math.rad
converts degrees to radians, I got confused with these CFrame.Angles
function.
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â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).