I am semi-new to scripting I know the basics. Ive been scripting for a year now. But I need help with a wall running script. I know people use raycasting but I dont know what that is. Help would be appreciated.
(Also please dont say I should work on something smaller first because im new. I already know.)
Your request is very vague so it will be hard to help without giving a full example, which is asking a lot
The basics would be using a Raycast on the player’s CFrame.RightVector
to detect walls and something from there. Do the same for the left side which should just be the negative right vector -CFrame.RightVector
.
i think he means this
also
You’re
that is REALLY vague, can you at the very least give some information like if you’ve already made a script but there’s an error or something or would you like to know how to make one
You’re best bet would be checking these out.
https://developer.roblox.com/en-us/api-reference/function/WorldRoot/Raycast
You can read the article on how to use Workspace:Raycast
Here’s a summary of how you perform raycasts.
The arguments for the :Raycast
function is :Raycast(origin:Vector3, direction:Vector3, raycastParams:RaycastParams)
. And the result is a RaycastResult
The RaycastResult
is the result of the raycast. If nothing was hit, it would return nil.
The origin is the starting position of the raycast.
The direction is, well the direction, except the distance is also measured in the given Vector3. Vector3.new(1,2)
is the same direction as Vector3.new(2,4)
, but their distance is different.
And lastly the raycastParams. This is used to filter the raycast, including what parts to ignore.
Let’s say you want to perform a raycast, from the character, to the direction its facing.
So let’s set up the variables
local player = game.Players.LocalPlayer
local character = player.Character
local hrp = character:WaitForChild("HumanoidRootPart")
Then the optional variables
local distance = 10 -- How far should the raycast go
Now we are ready to perform raycasts. In order to perform a raycast, you should give the arguments properly. I’m going to setup a function
function Raycast()
local result = workspace:Raycast()
return result
end
So first, lets give the origin (start position).
function Raycast()
local Origin = hrp.Position
local result = workspace:Raycast(Origin)
return result
end
Then the direction, multiplied by distance.
function Raycast()
local Origin = hrp.Position
local Direction = hrp.CFrame.LookVector
local result = workspace:Raycast(Origin, Direction*distance)
return result
end
If you don’t know what CFrame is, there are many tutorials about it.
Anyways, now we can just call the function and it will return the results.
So what can we do about the results. First of all, we can get the position where it hit using RaycastResult.Position
, and also the Part
that it hit with RaycastResult.Instance
. We can use this for our own code. Let’s say, whatever part is hit, it will turn to color Red
.
But before that, we need a way to call the function. How about, if the player clicks their left mouse button. So we include this is in our variables
local player = game.Players.LocalPlayer
local character = player.Character
local hrp = character:WaitForChild("HumanoidRootPart")
local mouse= player:GetMouse() -- This
Then the click event at the very bottom
mouse.Button1Up:Connect(function()
local result = Raycast()
end)
Then we use the result to change the color of whatever part is hit to Red
mouse.Button1Up:Connect(function()
local result = Raycast()
local part = result.Instance
part.Color = Color3.fromRGB(255,0,0)
end)
But wait, what if it hit nothing, it returns nothing and it will error.
Then let’s also check if the result exists
mouse.Button1Up:Connect(function()
local result = Raycast()
if result then
local part = result.Instance
part.Color = Color3.fromRGB(255,0,0)
end
end)
Final script
local player = game.Players.LocalPlayer
local character = player.Character
local hrp = character:WaitForChild("HumanoidRootPart")
local mouse= player:GetMouse()
local distance = 10 -- How far should the raycast go
function Raycast()
local Origin = hrp.Position
local Direction = hrp.CFrame.LookVector
local result = workspace:Raycast(Origin, DIrection*distance)
return result
end
mouse.Button1Up:Connect(function()
local result = Raycast()
if result then
local part = result.Instance
part.Color = Color3.fromRGB(255,0,0)
end
end)
Great! Now every time you click your mouse, it performs a raycast and whatever is hit will turn to color Red.
What about the RaycastParams, how can we use it?
First we should create a new RaycastParam object using RaycastParams.new()
Let’s add it in the optional variables
local distance = 10
local raycastParam = RaycastParams.new()
This creates a new object that we can modify. Let’s say you don’t want to hit a specific part, we can add it in the RaycastParam.FilterDescendants
table.
Yes, it is a table, and it says descendants, that means there can be multiple parts included, and all children of those parts are also included. Example, including your character model means you want the raycast to ignore all parts of your character.
So now we add all parts that should be ignored. How about a glass wall that is semi-transparent, named “Glass”.
local distance = 10
local raycastParam = RaycastParams.new()
raycastParam.FilterDescendants = {workspace.Glass} -- A table, that includes the glass wall
And now we include the raycastParam
into the raycast function
function Raycast()
local Origin = hrp.Position
local Direction = hrp.CFrame.LookVector
local result = workspace:Raycast(Origin, DIrection*distance, raycastParam) -- Third argument
return result
end
And now, everytime you cast a ray, it always ignores the glass wall we just made
Final Final Script
local player = game.Players.LocalPlayer
local character = player.Character
local hrp = character:WaitForChild("HumanoidRootPart")
local mouse= player:GetMouse()
local distance = 10 -- How far should the raycast go
function Raycast()
local Origin = hrp.Position
local Direction = hrp.CFrame.LookVector
local result = workspace:Raycast(Origin, DIrection*distance)
return result
end
mouse.Button1Up:Connect(function()
local result = Raycast()
if result then
local part = result.Instance
part.Color = Color3.fromRGB(255,0,0)
end
end)local player = game.Players.LocalPlayer
local character = player.Character
local hrp = character:WaitForChild("HumanoidRootPart")
local mouse= player:GetMouse()
local distance = 10
local raycastParam = RaycastParams.new()
raycastParam.FilterDescendants = {workspace.Glass}
function Raycast()
local Origin = hrp.Position
local Direction = hrp.CFrame.LookVector
local result = workspace:Raycast(Origin, DIrection*distance, raycastParam)
return result
end
mouse.Button1Up:Connect(function()
local result = Raycast()
if result then
local part = result.Instance
part.Color = Color3.fromRGB(255,0,0)
end
end)
Note: this is done in a local script, and the color changes are only seen by the player casting the ray.
Edit: spelling mistakes