I’m trying to make a tank, and to get the turret to rotate towards the mouse im using a bodygyro and setting the cframe to CFrame.new(gunBase.Position, mouse.hit.p). It works great, but after a while of not moving the tank or turret, gunBase.Postion returns NaN and completely breaks the tank.
Thing’s I’ve tried:
checking if gunBase.Position == gunBase.Position because checking to make sure something is not equal to itself is the only way I know of to detect NaN. I only update the bodygyro cframe if it’s not nan, but it still breaks everything
I’ve tried iterating through a for loop that contains the components of the cframe, and if any of them are NaN, then I don’t update the cframe, but of course, this still breaks everything.
I’ve looked into a post by @dthecoolest Vector3 values become NAN after period of time that seemed like he had a very similar issue, but his problem seemed to come from getting the .Unit of a vector with 0 magnitude, which I don’t think is quite my issue.
Here’s the section of code:
runService:BindToRenderStep("Aim", 100, function() local startPosition = gunBase.Position local endPosition = mouse.hit.p if mobile then local unitRay = camera:ScreenPointToRay(crosshair.AbsolutePosition.X + crosshair.AbsoluteSize.X/2, crosshair.AbsolutePosition.Y + crosshair.AbsoluteSize.Y/2) local hit, hitPosition = workspace:FindPartOnRay(Ray.new(unitRay.Origin, unitRay.Direction * 5000), character) endPosition = hitPosition end if startPosition == startPosition then gunGyro.CFrame = CFrame.new(startPosition, endPosition) else print(startPosition) end local result = workspace:Raycast(muzzle.Position, muzzle.CFrame.RightVector * -5000, gunFilter) if result then local vector, onScreen = camera:WorldToViewportPoint(result.Position) crosshair.Visible = onScreen crosshair.Position = UDim2.fromOffset(vector.X, vector.Y) end end)
If anyone has any idea on how to fix this or a better way to rotate a tank turret to your mouse, please let me know.