Need help with CFrame regrading bodygyro

Hi, im making a hover car, and I want to change one value of bodygyro.
Heres the line that is causing it

vehicle.Body.BodyGyro.CFrame = CFrame.new(math.rad(rayFront.Position.Y - rayBack.Position.Y),math.rad(body.BodyGyro.CFrame.Rotation.Y),math.rad(body.BodyGyro.CFrame.Rotation.Z))

Full code – Thank you in advance

local player = game.Players.LocalPlayer
local char = game.Workspace:WaitForChild(player.Name)
local humanoid = char:WaitForChild("Humanoid")
local sitanim = humanoid:LoadAnimation(script.SitAnim)
local driving = false
local UIS = game:GetService("UserInputService")
local tw = game:GetService("TweenService")
humanoid:GetPropertyChangedSignal("Sit"):Connect(function()
	if  humanoid.Sit == true then
		sitanim:Play()
		driving = true
		local vehicle = humanoid.SeatPart.Parent
		local body = vehicle.Body
		local params = RaycastParams.new()
		params.FilterType = Enum.RaycastFilterType.Exclude
		params.FilterDescendantsInstances = vehicle:GetChildren()
		game.ReplicatedStorage.Start:FireServer()
		repeat
			local ray = workspace:Raycast(body.Position,Vector3.new(0, -100, 0),params)
			local rayFront = workspace:Raycast(body.Front.WorldPosition,Vector3.new(0, -100, 0),params)
			local rayBack = workspace:Raycast(body.Back.WorldPosition,Vector3.new(0, -100, 0),params)
			vehicle.Body.BodyGyro.CFrame = CFrame.new(math.rad(rayFront.Position.Y - rayBack.Position.Y),math.rad(body.BodyGyro.CFrame.Rotation.Y),math.rad(body.BodyGyro.CFrame.Rotation.Z))
			if body.Position.Y < rayFront.Position.Y+5 then
				body.BodyVelocity.Velocity = Vector3.new(body.BodyVelocity.Velocity.X,1,body.BodyVelocity.Velocity.Z)
			else
				body.BodyVelocity.Velocity = Vector3.new(body.BodyVelocity.Velocity.X,-1,body.BodyVelocity.Velocity.Z)
			end
			task.wait()
		until driving == false
	else 
		driving = false
		sitanim:Stop()
		game.ReplicatedStorage.End:FireServer()
	end
end)
 
local a = false
local d = false
local w = false
local s = false
local speed = 0
UIS.InputBegan:Connect(function(key)
	if driving == true then
		local vehicle = humanoid.SeatPart.Parent
		local body = vehicle.Body
		local velocity = body.BodyVelocity
		
		if key.KeyCode == Enum.KeyCode.W then
			w = true
			repeat
				if vehicle.Pedal.Orientation.X > -40 then
				vehicle.Pedal.Orientation -= Vector3.new(1,0,0)
				end
				if speed < 100  then
				speed += 1 
				end
			velocity.Velocity = body.CFrame.LookVector*speed
			task.wait()
			until w == false
		elseif key.KeyCode == Enum.KeyCode.S then 
			s = true
			repeat
				if vehicle.Brake.Orientation.X > -40 then
					vehicle.Brake.Orientation -= Vector3.new(1,0,0)
				end
				if speed > 0 and w == false then
					if speed >= 5 then
						speed-=2	
					else
						speed = 0
					end
				end
				velocity.Velocity = body.CFrame.LookVector*speed
				task.wait()
			until s == false
		elseif key.KeyCode == Enum.KeyCode.A then
			a = true
			body.BodyGyro.CFrame *= CFrame.Angles(math.rad(0),math.rad(0),math.rad(0))
			vehicle.Wheel.HingeConstraint.TargetAngle = 45
			repeat 
				body.BodyGyro.CFrame *= CFrame.Angles(math.rad(0),math.rad(2),math.rad(0))
				task.wait()
			until a == false
		elseif key.KeyCode == Enum.KeyCode.D then
			d = true
			vehicle.Wheel.HingeConstraint.TargetAngle = -45
			repeat 
				body.BodyGyro.CFrame *= CFrame.Angles(math.rad(0),math.rad(-2),math.rad(0))
				task.wait()
			until d == false
		end
	end
	
end)
UIS.InputEnded:Connect(function(key)
	if driving == true then
		local vehicle = humanoid.SeatPart.Parent
		local body = vehicle.Body
		local velocity = body.BodyVelocity
		if key.KeyCode == Enum.KeyCode.W then
			w = false 
			repeat 
				vehicle.Pedal.Orientation += Vector3.new(2,0,0)
				task.wait()
			until vehicle.Pedal.Orientation.X >= 0 or w == true
			repeat 
				speed-=1
				velocity.Velocity = body.CFrame.LookVector*speed
				task.wait(0.1)
			until speed == 0 or s == true or w == true
		elseif key.KeyCode == Enum.KeyCode.S then 
			repeat 
				vehicle.Brake.Orientation += Vector3.new(2,0,0)
				task.wait()
			until vehicle.Brake.Orientation.X >= 0 or w == true
			s = false
		elseif key.KeyCode == Enum.KeyCode.A then
			body.BodyGyro.CFrame *= CFrame.Angles(math.rad(0),math.rad(0),math.rad(0))
			vehicle.Wheel.HingeConstraint.TargetAngle = 0
			a = false
		elseif key.KeyCode == Enum.KeyCode.D then
			vehicle.Wheel.HingeConstraint.TargetAngle = 0
			d = false
		end
	end

end)
2 Likes

In your code, you’re using the BodyGyro to control the rotation of the car. If you want to modify the values of the BodyGyro you can modify its properties.

-- Adjusted line to set CFrame for BodyGyro
local rotationY = math.rad(body.BodyGyro.CFrame.Rotation.Y)
local rotationZ = math.rad(body.BodyGyro.CFrame.Rotation.Z)
vehicle.Body.BodyGyro.CFrame = CFrame.Angles(0, rotationY, rotationZ)

This modification extracts the rotation values from body.BodyGyro.CFrame.Rotation and sets the CFrame using CFrame.Angles . This way, you can easily adjust the rotation values before setting the CFrame for the BodyGyro . Adjust the rotation values based on your requirements.

Here’s a modification to your code where I adjusted the line that sets the CFrame for the BodyGyro:

local player = game.Players.LocalPlayer
local char = game.Workspace:WaitForChild(player.Name)
local humanoid = char:WaitForChild("Humanoid")
local sitanim = humanoid:LoadAnimation(script.SitAnim)
local driving = false
local UIS = game:GetService("UserInputService")

humanoid:GetPropertyChangedSignal("Sit"):Connect(function()
    if humanoid.Sit == true then
        sitanim:Play()
        driving = true
        local vehicle = humanoid.SeatPart.Parent
        local body = vehicle.Body
        local params = RaycastParams.new()
        params.FilterType = Enum.RaycastFilterType.Exclude
        params.FilterDescendantsInstances = vehicle:GetChildren()
        game.ReplicatedStorage.Start:FireServer()

        repeat
            local rayFront = workspace:Raycast(body.Front.WorldPosition, Vector3.new(0, -100, 0), params)
            local rayBack = workspace:Raycast(body.Back.WorldPosition, Vector3.new(0, -100, 0), params)

            -- Adjusted line to set CFrame for BodyGyro
            local rotationY = math.rad(body.BodyGyro.CFrame.Rotation.Y)
            local rotationZ = math.rad(body.BodyGyro.CFrame.Rotation.Z)
            vehicle.Body.BodyGyro.CFrame = CFrame.Angles(0, rotationY, rotationZ)

            if body.Position.Y < rayFront.Position.Y + 5 then
                body.BodyVelocity.Velocity = Vector3.new(body.BodyVelocity.Velocity.X, 1, body.BodyVelocity.Velocity.Z)
            else
                body.BodyVelocity.Velocity = Vector3.new(body.BodyVelocity.Velocity.X, -1, body.BodyVelocity.Velocity.Z)
            end
            task.wait()
        until driving == false
    else
        driving = false
        sitanim:Stop()
        game.ReplicatedStorage.End:FireServer()
    end
end)

local a = false
local d = false
local w = false
local s = false
local speed = 0
UIS.InputBegan:Connect(function(key)
    if driving == true then
        local vehicle = humanoid.SeatPart.Parent
        local body = vehicle.Body
        local velocity = body.BodyVelocity

        if key.KeyCode == Enum.KeyCode.W then
            w = true
            repeat
                if vehicle.Pedal.Orientation.X > -40 then
                    vehicle.Pedal.Orientation -= Vector3.new(1, 0, 0)
                end
                if speed < 100 then
                    speed = speed + 1
                end
                velocity.Velocity = body.CFrame.LookVector * speed
                task.wait()
            until w == false
        elseif key.KeyCode == Enum.KeyCode.S then
            s = true
            repeat
                if vehicle.Brake.Orientation.X > -40 then
                    vehicle.Brake.Orientation -= Vector3.new(1, 0, 0)
                end
                if speed > 0 and w == false then
                    if speed >= 5 then
                        speed = speed - 2
                    else
                        speed = 0
                    end
                end
                velocity.Velocity = body.CFrame.LookVector * speed
                task.wait()
            until s == false
        elseif key.KeyCode == Enum.KeyCode.A then
            a = true
            body.BodyGyro.CFrame = body.BodyGyro.CFrame * CFrame.Angles(0, math.rad(2), 0)
            vehicle.Wheel.HingeConstraint.TargetAngle = 45
            repeat
                task.wait()
            until a == false
        elseif key.KeyCode == Enum.KeyCode.D then
            d = true
            vehicle.Wheel.HingeConstraint.TargetAngle = -45
            repeat
                body.BodyGyro.CFrame = body.BodyGyro.CFrame * CFrame.Angles(0, math.rad(-2), 0)
                task.wait()
            until d == false
        end
    end
end)

UIS.InputEnded:Connect(function(key)
    if driving == true then
        local vehicle = humanoid.SeatPart.Parent
        local body = vehicle.Body
        local velocity = body.BodyVelocity
        if key.KeyCode == Enum.KeyCode.W then
            w = false
            repeat
                vehicle.Pedal.Orientation += Vector3.new(2, 0, 0)
                task.wait()
            until vehicle.Pedal.Orientation.X >= 0 or w == true
            repeat
                speed = speed - 1
                velocity.Velocity = body.CFrame.LookVector * speed
                task.wait(0.1)
            until speed == 0 or s == true or w == true
        elseif key.KeyCode == Enum.KeyCode.S then
            repeat
                vehicle.Brake.Orientation += Vector3.new(2, 0, 0)
                task.wait()
            until vehicle.Brake.Orientation.X >= 0 or w == true
            s = false
        elseif key.KeyCode == Enum.KeyCode.A then
            body.BodyGyro.CFrame = body.BodyGyro.CFrame * CFrame.Angles(0, math.rad(-2), 0)
            vehicle.Wheel.HingeConstraint.TargetAngle = 0
            a = false
        elseif key.KeyCode == Enum.KeyCode.D then
            vehicle.Wheel.HingeConstraint.TargetAngle = 0
            d = false
        end
    end
end)

Hi, your fix did not work! the vehicle can only change 2 degrees to the left and right and stops.

fixed it nvm
Aaaaaaaaaaaaaaaaaa

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