Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make my ray move with the rotation of my bike wheel?

I am trying to make a bicycle script that uses raycasting to tell the script what my AssemblyLinerarVelocity should be. What I want to do is have a ray in the center of the first wheel and turn whenever the wheel turns but I do not know where to start.

Here is my code:

local bike = script.Parent
local vehicleseat = bike.VehicleSeat
local wheel1 = bike.Wheel1
local debounce = false
vehicleseat:GetPropertyChangedSignal("Occupant"):Connect(function()
	print("it works")
	local hum = vehicleseat.Occupant
	local char = hum.Parent
	vehicleseat:GetPropertyChangedSignal("Steer"):Connect(function()
		if not debounce then
			debounce = true
			wheel1.Orientation = Vector3.new(0,-45*vehicleseat.Steer,0)
			wait(0.05)
			debounce = false
		end
	end)
	vehicleseat:GetPropertyChangedSignal("Throttle"):Connect(function()

	end)
	local ray = Ray.new(wheel1.Position,wheel1.)
end)~~~~~~~~~~~~~~~~~


1 answer

In order to make the ray turn in conjunction with the wheel, you need to specify the direction of the ray relative to the orientation of the wheel. You can do this by using the CFrame of the wheel to transform a direction vector, such as Vector3.new(0,0,-1), which represents the direction of the ray when the wheel is oriented along the global z-axis.

Here is an example of how you could do this:

local bike = script.Parent
local vehicleseat = bike.VehicleSeat
local wheel1 = bike.Wheel1
local debounce = false
vehicleseat:GetPropertyChangedSignal("Occupant"):Connect(function()
    print("it works")
    local hum = vehicleseat.Occupant
    local char = hum.Parent
    vehicleseat:GetPropertyChangedSignal("Steer"):Connect(function()
        if not debounce then
            debounce = true
            wheel1.Orientation = Vector3.new(0,-45*vehicleseat.Steer,0)
            wait(0.05)
            debounce = false
        end
    end)
    vehicleseat:GetPropertyChangedSignal("Throttle"):Connect(function()

    end)
    local rayDirection = wheel1.CFrame:VectorToObjectSpace(Vector3.new(0,0,-1))
    local ray = Ray.new(wheel1.Position, rayDirection)
end)

In this example, the VectorToObjectSpace method is used to transform the direction vector Vector3.new(0,0,-1) from global coordinates to local coordinates relative to the CFrame of the wheel. This ensures that the direction of the ray will change as the orientation of the wheel changes.

This discussion is preserved as it was. New answers, comments, and votes are closed.