How can i check if part is moving backwards perpendicular to another part?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    No crazy backward speed “bug”.

  2. What is the issue? Include screenshots / videos if possible!
    I’m making a Tesla Model S, as you can see the collision avoidance works well when the car is moving slowly.

But, if it goes much faster than that, strange things happen.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

    if math.floor(Vehicle.DriveSeat.Velocity.Magnitude) == 0 or math.ceil(Vehicle.DriveSeat.Velocity.Magnitude) == 0 then
    Body.Autopilot.Origin.Force.Force = Vector3.new(0,0,0)
    end

1 Like

I am not really good at this, but I may think it is because of the speed it is going at. Considering searching on YouTube how to make it slow down when you Hit the wall or make it not move backwards at all.

Or not listen to me lmao.
(Edited: make it so when it comes flying in the wall it will disable the car completely If you can. If that dosent work then idk.) :wink:

Can we see your code for collision detection and braking? It’d help tremendously.

1 Like

Sure we can!
Here it is:

local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")

local Vehicle = script.Parent.Car.Value
local Body = Vehicle:WaitForChild("Body")
local Values = script.Parent:WaitForChild("Values")
local _Tune = require(Vehicle["A-Chassis Tune"])
local Origin = Body.Autopilot.Origin

local IgnoreFolder = Instance.new("Folder")
IgnoreFolder.Name = "IgnoreList"
IgnoreFolder.Parent = workspace

function CreateRayVisualizer(R,G,B)
	local RayPart = Instance.new("Part")
	RayPart.Transparency = 0.75
	RayPart.Name = "Ray"
	RayPart.Anchored = true
	RayPart.CanCollide = false
	RayPart.FormFactor = "Custom"
	RayPart.Color = Color3.fromRGB(R,G,B)
	RayPart.Material = Enum.Material.Neon
	RayPart.Parent = IgnoreFolder
	return RayPart
end

function UpdateRayVisualizer(R,G,B)
	local RayPart = IgnoreFolder.Ray
	RayPart.Color = Color3.fromRGB(R,G,B)
	return RayPart
end

local RayFront = CreateRayVisualizer(0,255,0)

function HandleRayDistance(Sensor, RayPart, Direction)
	--local NewRay = Ray.new(Sensor.Position, Sensor.CFrame:vectorToWorldSpace(Direction).unit * 200)
	local NewRay = Ray.new(Sensor.Position, Sensor.CFrame.lookVector * Vehicle.DriveSeat.Velocity.Magnitude/(math.pow((_Tune.RBrakeForce)*0.8, 0.1)))
	local Hit,RayHit = workspace:FindPartOnRay(NewRay, IgnoreFolder)
	local Distance = (Sensor.Position - RayHit).Magnitude
	
	RayPart.Size  = Vector3.new(0.05, 0.05, Distance)
	RayPart.CFrame = CFrame.new(Sensor.Position:Lerp(RayHit, 0.5), RayHit)
	
	return Hit
end

local Detected = false


while wait() do
	UpdateRayVisualizer(0,255,0)
	local Hit = HandleRayDistance(Origin, RayFront, Vector3.new(0, 0, -1))
	if Hit and not Detected and (math.sign(math.floor(Vehicle.DriveSeat.Velocity.Magnitude)) ~= 0 or -1) then
		for x = 1,math.huge do
		Detected = true
		UpdateRayVisualizer(255,0,0)
		script.Notify:Play()
		Body.Autopilot.Origin.Force.Force = Vector3.new(math.abs(Origin.CFrame.lookVector.X*_Tune.FBrakeForce*3)*-1, (Origin.CFrame.lookVector.Y*_Tune.FBrakeForce*3)*-1, (Origin.CFrame.lookVector.Z*_Tune.FBrakeForce*2)*-1)
		for y = 1,math.huge do
			wait()
			
			if math.floor(Vehicle.DriveSeat.Velocity.Magnitude) == 0 or math.ceil(Vehicle.DriveSeat.Velocity.Magnitude) == 0 then
				Body.Autopilot.Origin.Force.Force = Vector3.new(0,0,0)
				break
			end
		end
		Detected = false
		break
		end
	end
end

Here we go:

Issue 1: You are using,

while wait() do

Which can cause lag and is not recommended whatsoever. Instead, use,

while wait(0.1)

or something of similar wait time for your script.

The solution to your issue is when you check to stop braking. What you want to do is compare your car’s current velocity with a Vector3 value, such as (1,1,1), as so,

local velocity = Vehicle.DriveSeat.Velocity

if velocity < Vector3.new(1,1,1) then
--stop braking
end
1 Like