RayCast dash system works bad

(Bad English Warning)

From Youtube tutorial I am trying to make dash system using RayCast
But it only worked with front dash so i decided to upgrade script
The poblem is that left and right dashes do not work but front and back dashes work great

local RS=game:GetService("ReplicatedStorage")

local DS=game:GetService("Debris")

local event=RS.Remotes:WaitForChild("DashEvent")
local DashSound=RS.Sounds:WaitForChild("Dash")
local PS=game:GetService("PhysicsService")


local listOfDebounce={}
for _,player in pairs(game:GetService("Players"):GetChildren()) do
   listOfDebounce[player]=false
end

local ignoreList = {}
for _,v in pairs(workspace:GetDescendants())do
   if v:IsA("BasePart") or v:IsA("WedgePart") or v:IsA("UnionOperation") then
   	ignoreList[#ignoreList+1]=v
   end
end

function NoCollide(model,Group)
   for k,v in pairs (model:GetChildren()) do
   	if v:IsA"BasePart" then
   		PS:SetPartCollisionGroup(v,Group)

   	end
   end
end


local function GetDirection(part,front,left,back,right)
   if front then
   	return part.CFrame.LookVector*130+Vector3.new(0,5,0)
   elseif left then
   	return part.CFrame.RightVector*-130+Vector3.new(0,5,0)
   elseif back then
   	return part.CFrame.LookVector*-130+Vector3.new(0,5,0)
   elseif right then
   	return part.CFrame.RightVector*130+Vector3.new(0,5,0)
   else
   	return part.CFrame.LookVector*130+Vector3.new(0,5,0)
   end
end

local function tunrOnOff(Table,switch)
   for _,particle in pairs(Table) do
   	particle.Enabled=switch
   end
end

local function treeVisibility(character,transparency)
   local tool=character:FindFirstChildWhichIsA("Tool")
   if tool then
   	tool.Blade.Trail.Enabled=not tool.Blade.Trail.Enabled
   end
   for i,v in pairs(character:GetDescendants()) do 
   	if v:IsA("BasePart") or v:IsA("Decal") then 
   		if v.Name=="HumanoidRootPart" or v.Name=="ShadowsDashes" then
   			continue
   		end
   		v.Transparency=transparency
   	end
   end
end

event.OnServerEvent:Connect(function(player,W,A,S,D,shiftLock)
   
   local character=player.Character
   local humanoid=character:FindFirstChild("Humanoid")	
   local humanoidRootPart=character:FindFirstChild("HumanoidRootPart")
   local afterImageParticles=character:FindFirstChild("ShadowsDashes"):GetChildren()
   local attaking=character:GetAttribute("Attaking")
   local stunned=character:GetAttribute("Stunned")
   if attaking or stunned or listOfDebounce[player] then
   	return
   end
   character:SetAttribute("Stunned",true)
   listOfDebounce[player]=true

   treeVisibility(character,1)
   tunrOnOff(afterImageParticles,true)
   NoCollide(character,"Dashing")

   local clone=DashSound:Clone()
   clone.Parent=humanoidRootPart
   clone:Play()
   game:GetService("Debris"):AddItem(clone,clone.TimeLength)
   
   local ray=Ray.new(humanoidRootPart.Position,GetDirection(humanoidRootPart,W,A,S,D))
   local rayPosition,hitPostition = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)
   print(ray)

   local bodyPosition=Instance.new("BodyPosition")
   bodyPosition.Name="DashForce"
   bodyPosition.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
   bodyPosition.D=500
   bodyPosition.P=400
   bodyPosition.Position=hitPostition

   local bodyGyro=Instance.new("BodyGyro")
   bodyGyro.Name="DashOrientation"
   bodyGyro.MaxTorque=Vector3.new(math.huge,math.huge,math.huge)
   bodyGyro.D=500
   bodyGyro.P=300
   bodyGyro.CFrame=humanoidRootPart.CFrame

   bodyGyro.Parent=humanoidRootPart
   bodyPosition.Parent=humanoidRootPart

   task.wait(0.3)
   
   bodyGyro:Destroy()
   bodyPosition:Destroy()

   
   treeVisibility(character,0)
   tunrOnOff(afterImageParticles,false)
   NoCollide(character,"p")
   character:SetAttribute("Stunned",false)
   task.wait(3)
   listOfDebounce[player]=false
end)

The main issue is in GetDirection function I don’t understand well
CFrame.RightVector
Please give me some advice

1 Like

I did some tests and found out that dashes to the left and right work but only in the air

I tried to teleport character to end postion of ray and it works even when character stand on flor but I still not understand why it does not work with body position and body gyro

I solved this problem by adding 0.4 studs to Y axis before dashing
It works but I am still waiting for answer for my first question :weary: :weary: :weary:

Maybe it doesn’t work with BodyGyro and BodyPosition because they’re deprecated methods (I don’t know, just guessing).

Try using (instead of Ray.new) workspace:Raycast(position, direction, params). If it hits something, it’ll return Instance (the part that it collided), else it will return nil.

But I think it won’t fix your script. Try to find what is going wrong when you side-dash. Is it because the ray is returning nil or another reason? Finding what is wrong must help you to fix it. If you realize why it isn’t working, update this post.

1 Like

The problem was that ray is hitting parts of character and because of that BodyPosition.Position sets
to position which is close to humanoidRootPart position
I solved it already

To be honest that was dumb from my side to not check the output of rayPosition

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