Issue with clamping desired angle car system

I believe this is the issue. It keeps the steering input to -1. No change either as it is all ran inside a loop.

Below is the math that calculates the ai to move either left or right and towards the target.

Its self explanatory so I don’t think I will need to explain the works exactly.


I have done the liberty and given you the outputs of each print you see before you which will be in the form of comments on the right of each invoke.

ignore the timestamps as they all execute seemingly at the same time.

I am not the best with math. Please excuse me.


local angleY, angleX, angleZ =  seat.CFrame:ToEulerAnglesYXZ() -- get the angles
local desiredAngle = math.deg(rot) - angleY + 90 -- do math
		
print(desiredAngle) --   20:49:06.605  -43.945911908380424  -  Studio
print(math.deg(rot) - angleY + 90) -- same as above quite literally

if desiredAngle > 180 then
	desiredAngle = desiredAngle - 360
elseif desiredAngle < -180 then
	desiredAngle = desiredAngle + 360
end

local steeringInput = math.clamp(desiredAngle / 90, -1, 1)
print(steeringInput) -- 20:49:06.605  -0.48828791009311584  -  Studio
local throttleInput = 1

if (bumper.WorldPosition - target).Magnitude < 10 then
	throttleInput = 0
end

Inputs:SetAttribute("steeringInput", steeringInput)
Inputs:SetAttribute("throttleInput", throttleInput)
1 Like

It seems like the logic expects desiredAngle to be larger, or more aligned. In that case, you probably don’t want math.deg(rot) because rot is already in degrees. You can simplify this calculation to just remove the math.deg conversion. Make sure rot and angleY are using the same unit. If needed, you can try to adjust the angle offsets to make the steering input more responsive.

2 Likes

Thanks once more. I will take what you’ve said and give them ago thanks. Do expect a reply or mention if I do find any issues.

You’re welcome! I’ll be happy to help again. Just let me know how it goes.

1 Like

I didn’t realise its been 4 hours.

I have rewrote the way it will turn, instead of relying on another part of the car to do it we will instead handle it in house instead.

The car still rotates like it does however I feel this current rewrite (all be it fairly similar) is a lot better and easier to edit.


local rot = math.atan2(target.Z - bumper.Position.Z, target.X - bumper.Position.X) -- calculate rotate
local offset = 90
local angleY, angleX, angleZ =  seat.CFrame:ToEulerAnglesYXZ() -- get the angles
local desiredAngle = math.deg(rot) + seat.Orientation.Y + 90 -- old code for testing
--math.deg(rot) - angleY - target.Y -- do math
		
--[[if desiredAngle > 180 then
	desiredAngle = -( 360 - desiredAngle)
elseif desiredAngle < -180 then
	desiredAngle = 360 + desiredAngle
end	]]

if desiredAngle > 180 then
	--print(desiredAngle - 360)
	desiredAngle = desiredAngle - 360
elseif desiredAngle < -180 then
	--print(desiredAngle + 360)
	desiredAngle = desiredAngle + 360
end
		
local steeringInput = math.clamp(desiredAngle / offset, -1, 1)
local throttleInput = Inputs:GetAttribute("throttleInput")

local maxSpeed = math.max(Engine:GetAttribute("forwardMaxSpeed"), Engine:GetAttribute("reverseMaxSpeed"))
local speed = math.min(controller:getChassisForwardSpeed(), maxSpeed)
		
-- Steering is reduced at higher speeds to provide smoother handling
local steeringFactor = math.max(1 - (speed / maxSpeed ) * Steering:GetAttribute("steeringReduction"), 0)
local steeringAmount = (steeringInput * steeringFactor) -- dont really wanna touch this tbh
		
print(steeringAmount)
print(steeringAmount * SteeringRack.LowerLimit)
print(steeringAmount * SteeringRack.UpperLimit)
		
if steeringAmount ~= 0 then
			
	if throttleInput == 0 then
		throttleInput = 1
	end
			
	if steeringInput == 1 then
		print(1)
		SteeringRack.TargetPosition = steeringAmount * SteeringRack.LowerLimit
	elseif steeringInput == -1 then
			print(-1)
			SteeringRack.TargetPosition = -steeringAmount * SteeringRack.UpperLimit
	elseif steeringInput == 0 and not (steeringInput < 0) and not (steeringInput > 0) then
			print(0)
			SteeringRack.TargetPosition = 0
	end
			
else
	SteeringRack.TargetPosition = 0
end

Did some searching, Found out that you can quite literally just set a number directly to the steering and it will act on that. I forgot it did that.

Solution:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.