I guess that I am basically making this post because I am unsure if I would be able to Raycast strictly forward. I believe that LookVector already does this and I am unsure if there is a way for me to get around this issue. I am not sure if using CFrame.Z would make any difference, so I am just asking for opinions.
If anyone can give me any insight then that’ll be great!
I think what you may be asking is how to flatten onto the xz plane while keeping the vector at a length of 1 (a unit vector).
I often use this code:
local vec = humanoidRootPart.CFrame.lookVector -- or use camera.CoordinateFrame.lookVector
local flatVec = vec * Vector3.new(1,0,1) -- Doesn't have a length of 1
local flatVecUnit = flatVec.unit -- Does have a length of 1
-- The check below is necessary to make sure that the vector didn't get corrupt when being unitized.
if flatVecUnit.x ~= flatVecUnit.x then flatVecUnit = Vector3.new(0,0,-1) end
This is code that I change using your variable: – Probably the only one you have to look at
Summary
function forwardlatch() -- Is is possible to get the direction locally
local origin = HRP.Position + Vector3.new(0,2,0)
local direction = HRP.CFrame.LookVector * 20 + HRP.Position
local result = Workspace:Raycast(origin, direction, rayParams)
if result then
latchvalue = true
results = result.Normal
pos = result.Position
typ = result.Instance
pcframe = CFrame.new(pos)
SurfaceCFrame = CFrame.new(result.Position, result.Position+result.Normal)*CFrame.Angles(math.rad(-90),85,0)
end
if results == nil then
latchvalue = false
return
end
if results ~= nil then
print("normal latching began")
latching()
end
end
Inside of that code ^; the latching() function is called, this is it:
Summary
function latching()
print(latchvalue)
latchingToWall = results
if typ == workspace.Terrain then
print("Latching to Terrain")
HRP.CFrame = SurfaceCFrame:ToWorldSpace(pOffset)
else
HRP.CFrame = pcframe
end
wait(.1)
latchingToWall = nil
results = nil
pos = nil
pcframe = nil
typ = nil
SurfaceCFrame = nil
latchvalue = false
end
And then this is the dash function, where the forwardlatch() function is called:
Summary
local Tapped = false
local DoubleTapTime = 0.2
local CoolDown = 1.5
local LastTime = tick()
local LastTime2 = tick()
local LastTime3 = tick()
local LastTime4 = tick()
local DB = false
local Dashing = false
UIS.InputBegan:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.W then
local now = tick()
local difference = (now - LastTime)
if difference <= DoubleTapTime and not DB then
delay(.4, function()
Dashing = false
end)
local BV = Instance.new("BodyVelocity", character.PrimaryPart)
BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
BV.Velocity = Vector3.new(0,0,0)
BV.Name = "DashForce"
Dashing = true
local latchcon
latchcon = RunService.Heartbeat:Connect(function()
if Dashing then
forwardlatch()
else
latchcon:Disconnect()
end
end)
local Connection
Connection = game:GetService('RunService').Heartbeat:Connect(function()
local Suc, Err = pcall(function()
if Dashing == true then
DB = true
BV.Velocity = Cam.CFrame.lookVector * 75
local Gravity = Controller.GravityUp
if (Gravity-Vector3.new(0, 1, 0)).Magnitude > .1 then
Dashing = false
end
elseif Dashing == false then
Connection:Disconnect()
BV:Destroy()
wait(CoolDown)
DB = false
Dashing = false
end
end)
if not Suc and Err then
print(Err)
Connection:Disconnect()
end
end)
end
LastTime = tick()
end
end)
Sorry if it’s confusing, I use the latch functions in a lot of other functions so this simplifies it for me.
if difference <= DoubleTapTime and not DB then
delay(.4, function()
Dashing = false
end)
local BV = Instance.new("BodyVelocity", character.PrimaryPart)
BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
BV.Velocity = Vector3.new(0,0,0)
BV.Name = "DashForce"
Dashing = true
local latchcon
latchcon = RunService.Heartbeat:Connect(function()
if Dashing then
forwardlatch()
else
latchcon:Disconnect()
end
end)
local Connection
Connection = game:GetService('RunService').Heartbeat:Connect(function()
local Suc, Err = pcall(function()
if Dashing == true then
DB = true
BV.Velocity = Cam.CFrame.lookVector * 75
local Gravity = Controller.GravityUp
if (Gravity-Vector3.new(0, 1, 0)).Magnitude > .1 then
Dashing = false
end
elseif Dashing == false then
Connection:Disconnect()
BV:Destroy()
wait(CoolDown)
DB = false
Dashing = false
end
end)
if not Suc and Err then
print(Err)
Connection:Disconnect()
end
end)
end
I am definitely pretty new to scripting so I don’t really understand anything I make fully
That said, can you tell me why you added + HRP.Position and what that does exactly?
It seems to me that it’s casting 20 studs out of the LookVector, but then I am not sure how to + HRP.Position changes this.
EDIT:
As a side note;
I feel like the best way to achieve what I want to happen, would be to limit the lookVector to be flat, as @VitalWinter suggested. However, I am unsure of how to incorporate that into the code I already have.
Hmm I am not too sure what the + HRP.Position does either but it seems to work for parts (without it it doesn’t), perhaps try removing it for the raycasting.
If it works that way I’m not sure why it is different.
I’ve seemed to have figure out how to resolve me issue thanks to @VitalWinter
Here is the ending code:
Summary
function forwardlatch() -- Is is possible to get the direction locally
local origin = HRP.Position
local vec = HRP.CFrame.LookVector
local flatvec = vec * Vector3.new(1,0,1)
local direction = flatvec * 20
local result = Workspace:Raycast(origin, direction, rayParams)
if result then
latchvalue = true
results = result.Normal
pos = result.Position
typ = result.Instance
pcframe = CFrame.new(pos)
SurfaceCFrame = CFrame.new(result.Position, result.Position+result.Normal)*CFrame.Angles(math.rad(-90),85,0)
end
if results == nil then
latchvalue = false
return
end
if results ~= nil then
print("normal latching began")
latching()
end
end
I had to limit the Raycast direction to only the X and Y axis.
I did this with VitalWinter’s code and his help by including this code:
local origin = HRP.Position
local vec = HRP.CFrame.LookVector
local flatvec = vec * Vector3.new(1,0,1)
local direction = flatvec * 20