From 14f7b7bd4af11c95938c859ac40b4462f4218937 Mon Sep 17 00:00:00 2001 From: HRiggs Date: Thu, 12 Feb 2026 11:07:04 -0500 Subject: [PATCH] Update Towbar Height --- 42.13/media/lua/client/TowBar/TowingUtils.lua | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/42.13/media/lua/client/TowBar/TowingUtils.lua b/42.13/media/lua/client/TowBar/TowingUtils.lua index d143542..d757eef 100644 --- a/42.13/media/lua/client/TowBar/TowingUtils.lua +++ b/42.13/media/lua/client/TowBar/TowingUtils.lua @@ -8,6 +8,21 @@ TowBarMod.Utils.tempVector2 = Vector3f.new() --- Util functions --------------------------------------------------------------------------- +--- Compute the attachment Y offset for a vehicle so the towbar sits just +--- above the wheels (i.e. a fixed distance off the ground) regardless of +--- how the vehicle model is configured. +local function computeAttachmentHeight(vehicle) + local script = vehicle:getScript() + if not script then return -0.5 end + + local wheelCount = script:getWheelCount() + if wheelCount > 0 then + return script:getWheel(0):getOffset():y() + 0.1 + end + + return -0.5 +end + function TowBarMod.Utils.isTrailer(vehicle) return string.match(string.lower(vehicle:getScript():getName()), "trailer") end @@ -88,6 +103,19 @@ function TowBarMod.Utils.updateAttachmentsForRigidTow(towingVehicle, towedVehicl towedAttachment:setUpdateConstraint(false) towedAttachment:setZOffset(0) + -- Dynamic height: compute Y from wheel offset so the towbar never clips the floor. + local towingHeight = computeAttachmentHeight(towingVehicle) + local towedHeight = computeAttachmentHeight(towedVehicle) + + -- Store and update the towing vehicle's attachment Y. + local towingModData = towingVehicle:getModData() + if towingModData["towBarOriginalTowingOffsetY"] == nil then + towingModData["towBarOriginalTowingOffsetY"] = towingAttachment:getOffset():y() + towingModData["towBarOriginalTowingAttachmentId"] = attachmentA + end + local towingOffset = towingAttachment:getOffset() + towingAttachment:getOffset():set(towingOffset:x(), towingHeight, towingOffset:z()) + local towedModData = towedVehicle:getModData() local spacingDistance = 1.0 if TowBarMod.Config and tonumber(TowBarMod.Config.rigidTowbarDistance) ~= nil then @@ -114,22 +142,35 @@ function TowBarMod.Utils.updateAttachmentsForRigidTow(towingVehicle, towedVehicl local zDirection = baseZ >= 0 and 1 or -1 local zShift = zDirection * spacingDistance - towedAttachment:getOffset():set(baseX, baseY, baseZ + zShift) + towedAttachment:getOffset():set(baseX, towedHeight, baseZ + zShift) towedModData["isChangedTowedAttachment"] = true towedModData["towBarChangedAttachmentId"] = attachmentB towedModData["towBarChangedOffsetZShift"] = zShift towedVehicle:transmitModData() + towingVehicle:transmitModData() end function TowBarMod.Utils.updateAttachmentsOnDefaultValues(towingVehicle, towedVehicle) - local towingAttachmentId = towingVehicle:getTowAttachmentSelf() + local towingModData = towingVehicle:getModData() + local towingAttachmentId = towingModData["towBarOriginalTowingAttachmentId"] + or towingVehicle:getTowAttachmentSelf() local towingAttachment = towingVehicle:getScript():getAttachmentById(towingAttachmentId) if towingAttachment ~= nil then towingAttachment:setUpdateConstraint(true) local zOffset = (towingAttachmentId == "trailer") and -1 or 1 towingAttachment:setZOffset(zOffset) + + -- Restore the original Y offset that was overridden by dynamic height. + local originalY = tonumber(towingModData["towBarOriginalTowingOffsetY"]) + if originalY ~= nil then + local off = towingAttachment:getOffset() + towingAttachment:getOffset():set(off:x(), originalY, off:z()) + end end + towingModData["towBarOriginalTowingOffsetY"] = nil + towingModData["towBarOriginalTowingAttachmentId"] = nil + towingVehicle:transmitModData() local towedModData = towedVehicle:getModData() local changedAttachmentId = towedModData["towBarChangedAttachmentId"] or towedVehicle:getTowAttachmentSelf()